Hello I'm using the latest WP version, WITH WooCommerce.

I have a plugin that allow users, from the product page, to upload images. The images are uploaded as archive zip files.

I need users to have the option to make effects to images from media gallery using plugin such as:


The problem is that all the images uploaded with that plugin are zipped, and being so they are not in the database. So wordpress doesn't have a clue that the images are on the server and who uploaded them.

So I wrote a little PHP script that extracts all files to the media folder.

The problem now is that all users are able to see the media inside that folder.

Is there a way to write a small plugin that use the user name and tells WP that the media folder for that user is located in same folder as user name example:

username: testusr and when testusr going to media library wordpress tell that the folder for that user is inside "uploads/testusr"

So users are able to only see their images.

I know I can change wp-config.php but it's a global setting and I'm looking for a per user setting.

  • WordPress media whenever uploaded gets assigned an author in the database. If you've kept the same author then you can filter to only show images from that author - see this question.
    – Howdy_McGee
    Sep 13, 2016 at 15:09
  • the images are ziped and uploaded with plugin for woocommerce and when i wrote php to unziped it how can i assigned it to author . the only option i see is write php script that unzip all the images to folder then write to db the images name and assigned it to author id, any other suggestion?
    – M.mik
    Sep 13, 2016 at 15:10
  • If they're accessible from the Media Library it sounds like they would be assigned to the database in some way, right?
    – Howdy_McGee
    Sep 13, 2016 at 15:11
  • i just checked they not showing in media libary, i guess the only way is php to read files name and write it to db with current user id.
    – M.mik
    Sep 13, 2016 at 15:15
  • 1
    If you wrote a script to extract to media folder, try to run that script from within WP. Then you would need to assign an author ID for those unzipped uploads. You might want to check wp-cli for this task because you would also need to create the attachment post type so WP can link the attachament to the post. WP-CLI would make it possible to run all this via command line so it's easier to automate the process
    – bynicolas
    Sep 13, 2016 at 15:16

1 Answer 1


the easy way is using this:

// $filename should be the path to a file in the upload directory.
$filename = '2016/09/1.jpg';

// The ID of the post this attachment is for.
$parent_post_id = 32;

// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );

// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();

// Prepare an array of post data for the attachment.
$attachment = array(
    'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
    'post_mime_type' => $filetype['type'],
    'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
    'post_content'   => '',
    'post_status'    => 'inherit'

// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );

// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );

// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );

set_post_thumbnail( $parent_post_id, $attach_id );

using this working good the only problem is no thumbnails other than that work great

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.