9

I want to list images for only user uploaded image. Here is the scenario:

  1. Using the image uploader on front end using iframe.
  2. I have added upload_files cap to subscriber level users and want them to see only images they uploaded.
  3. I've found this Q Restricting users to view only media library items they have uploaded? but the accepted answer not working for me.

    // Code originally by @t31os
    add_action('pre_get_posts','users_own_attachments');
    
    function users_own_attachments( $wp_query_obj ) 
    {
        global $current_user, $pagenow;
    
        if( !is_a( $current_user, 'WP_User') )
            return;
    
        if( 'upload.php' != $pagenow )
            return;
    
        if( !current_user_can('delete_pages') )
            $wp_query_obj->set('author', $current_user->id );
    
        return;
    }
    
  4. The frontend page i am using the image uploader is named "Entry".

  5. If i remove this part of the code if( 'upload.php' != $pagenow ) return; the code works. I i think i have to improve that part of the conditional code but could not figure it out. The conditional is important because i don't want the code to apply on other pages where it not required.

So, i need help to improve the conditional part as i mentioned. Thanks!

  • 1
    If you're copy/pasting code, please do the following: If you only wrapped your code in a function name, please add the proper amount of spaces so function doesn't align with the fn content. – kaiser May 6 '12 at 21:36
5

This works for me in order to list the items uploaded by a user on the media library.

function users_my_media_only( $wp_query ) {
    if ( false !== strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) ) {
        $current_user = wp_get_current_user();
        $current_user = $current_user->ID;
        if ( ! current_user_can( 'manage_options' ) ) {
            global $current_user;
            $wp_query->set( 'author', $current_user->id );
        }
    }
}
add_filter('parse_query', 'users_my_media_only' );

It will allow all users with the manage_options capability (usually admin) to see all images, the other users will see only their own images. Note that a few improvements can be made like $pagenow and current_user_can. Not a beauty but it does the job

  • 2
    Ideally you'd try to compare to some part of the wp_upload_dir() result. – kaiser May 7 '12 at 6:22
2

The media upload form that i was pulling up via thickbox was media-upload.php. So, i had to change the conditional to if( 'upload.php' != $pagenow && 'media-upload.php' != $pagenow). I forgot to look into the obvious place :)

// Code originally by @t31os
add_action('pre_get_posts','users_own_attachments');

function users_own_attachments( $wp_query_obj ) 
{
    global $current_user, $pagenow;

    if( !is_a( $current_user, 'WP_User') )
        return;

    if( 'upload.php' != $pagenow && 'media-upload.php' != $pagenow)
        return;

    if( !current_user_can('delete_pages') )
        $wp_query_obj->set('author', $current_user->id );

    return;
}

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