Search code examples
wordpressmedia-library

Wordpress - Copy media library or categorize?


I have a plugin in mind for Wordpress (that I'm aiming to create) that bascially are going to use the same features of the media - library (uploading / viewing / setting descriptions etc on images).

Is there a way of "copying" the media library and then use it/modifiy it as you wish?

Background: I want to categories these photos for different customers (that the owner of the site uploads). I believe the built-in media library should be used for site-specific photos (like logos, product photos etc). I don't want mix built-in "media-library" with specific customers photos.

Guidelines Maybe there's a better approach. (Is it better to create categories in the media-categories for each customer?) If there is, please tell me. I'm looking for guidelines - not a solution.


Solution

  • Solution 1: First query posts, and then for each one of them use something like this code:

    $images = get_children( array(
        'post_parent'    => get_the_ID(),
        'post_type'      => 'attachment',
        'post_mime_type' => 'image',
        'numberposts'    => -1
        ) );
    foreach ( (array) $images as $image ) {
        print wp_get_attachment_url( $image->ID );
    }
    

    I don't like this solution because it will generate many SQL queries.

    Solution 2: Write your own SQL query which will select all images at once. It should look something like this:

    // change this to your custom post type, attachment in your case.
    $post_type = 'attachment';
    
        global $wpdb;
        $where = get_posts_by_author_sql( $post_type );
        $query = "SELECT * FROM $wpdb->posts p where p.post_type = 'attachment' AND (p.post_mime_type LIKE 'image/%')  AND (p.post_status = 'inherit') AND p.post_parent IN (SELECT $wpdb->posts.ID FROM $wpdb->posts  {$where} ) ORDER BY p.post_date DESC";
        $results =  $wpdb->get_results( $query );
    
        if ( $results ) {
    
          foreach ( (array) $results as $image ) {
            print wp_get_attachment_url( $image->ID );
          }
        }
    

    The way I would build that plugin, is creating a custom post type, similar to the media upload type, and then modify it as you wish (adding a category for example).

    I like the idea of the plugin, so let me you know then you finish, Good luck.

    Edit: If you are actually want to copy all the image files to another directory you could do something like this using :

    $source = wp_upload_dir(); // get WordPress upload directory
    $dest= "wp-content/My-new-Directory"; // where you would like to copy
    
    mkdir($dest, 0755);
    foreach (
     $iterator = new \RecursiveIteratorIterator(
      new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
      \RecursiveIteratorIterator::SELF_FIRST) as $item
    ) {
      if ($item->isDir()) {
        mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
      } else {
        copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
      }
    }