• Hi, I hope some knowledgeable peeps here can help with a simple issue I’m having.

    I host a multi author site and I’ve restricted their media access to their own in the backend when adding a new post by using

    //restrict authors to only being able to view media that they've uploaded
    function ik_eyes_only( $wp_query ) {
    	//are we looking at the Media Library or the Posts list?
    	if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false
    	|| strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) {
    		//user level 5 converts to Editor
    		if ( !current_user_can( 'level_5' ) ) {
    			//restrict the query to current user
    			global $current_user;
    			$wp_query->set( 'author', $current_user->id );
    		}
    	}
    }
    
    //filter media library & posts list for authors
    add_filter('parse_query', 'ik_eyes_only' );

    This works fantastic, the only issue I’m having right now is that when they click to “set featured image” (part of my theme) they can see access all authors uploaded images, which yes is bad. How can I restrict them to only see their images when accessing the set featured image feature?

    Thanks for any help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter lildragn

    (@lildragn)

    Hi again, sorry for the bump, but somewhere here must be privy to this issue? There must be a way.

    Thanks

    Thread Starter lildragn

    (@lildragn)

    Just in case anyone ever wanted a solution to this I’ve “hacked” together some existing code and it seems to work albeit ham-fisted ??

    //restrict authors to only being able to view media that they've uploaded
    function ik_eyes_only( $wp_query ) {
    	//are we looking at the Media Library or the Posts list?
    	if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false
    	|| strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) {
    		//user level 5 converts to Editor
    		if ( !current_user_can( 'level_5' ) ) {
    			//restrict the query to current user
    			global $current_user;
    			$wp_query->set( 'author', $current_user->id );
    		}
    	}
    }
    
    //filter media library & posts list for authors
    add_filter('parse_query', 'ik_eyes_only' );
    
    add_action('pre_get_posts','ml_restrict_media_library');
    
    function ml_restrict_media_library( $wp_query_obj ) {
        global $current_user, $pagenow;
        if( !is_a( $current_user, 'WP_User') )
        return;
        if( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
        return;
        if( !current_user_can('manage_media_library') )
        $wp_query_obj->set('author', $current_user->ID );
        return;
    }

    Cheers

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Restrict author to their own media (Featured Image)’ is closed to new replies.