0

I am letting users upload avatars which are stored within the uploads folder inside the avatars subfolder which I created.

The problem is that avatars are now listed in the media library among other uploaded images.

How can I prevent that?

I was looking for a filter to filter out what is being displayed but couldn't find one. I wouldn't mind the avatars folder being outside the uploads folder if it has to.

2 Answers 2

0

Like most page loads in WordPress, WP_Query is intimately involved meaning pre_get_posts is your friend. Proof of concept:

function step_2($qry) {
  $qry->set('post__not_in',array(468,303));
}
function step_1() {
  add_action('pre_get_posts','step_2');
}
add_action('load-upload.php','step_1');

I'm using the load-upload.php hook to isolate the filter to the "Library" page. If you've changed the uploads folder for your avatars you've already got some "upload" code in place. You will need to extend that to track your avatar IDs and retrieve them instead of hard-coding as I did. It might be possible to use some other mechanism to filter the results as well.

While I haven't tested this, if you were to upload as some other post type than "attachment" the rest would probably fall into place without further effort.

1
  • I ended up using the wp_handle_upload function instead of wp_media_upload which does not create attachments. Your last sentence guided me on the right path. Don't know how I didn't figure it out by myself. Wasn't my day yesterday. Thanks.
    – Ivannnnn
    Commented Jan 1, 2016 at 9:16
0

Maybe How can I hide media library images from general users? relates a bit to your current problem. In the case below, if a user isn't an admin they can only see their personally uploaded images.

This trick could also work by attaching metadata on the image after uploading to signal it was a special upload -- then modify the query to exclude items with that special meta.

function hide_posts_media_by_other($query) {
    global $pagenow;
    if( ( 'edit.php' != $pagenow && 'upload.php' != $pagenow   ) || !$query->is_admin ){
        return $query;
    }
    if( !current_user_can( 'manage_options' ) ) {
        global $user_ID;
        $query->set('author', $user_ID );
    }
    return $query;
}
add_filter('pre_get_posts', 'hide_posts_media_by_other');

/*
 * Hide Media Images
 */
function hide_attachments_wpquery_where( $where ){
    global $current_user;
    if( !current_user_can( 'manage_options' ) ) {
        if( is_user_logged_in() ){
            if( isset( $_POST['action'] ) ){
                // library query
                if( $_POST['action'] == 'query-attachments' ){
                    $where .= ' AND post_author='.$current_user->data->ID;
                }
            }
        }
    }
    return $where;
}
add_filter( 'posts_where', 'hide_attachments_wpquery_where' );

// via: http://phpbits.net/hide-wordpress-posts-and-media-uploaded-by-other-users/

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.