• If you search for this topic, there’s *plenty* discussions out there, but none seem to cover the new way the Media Library is displayed in WP v4.0. The Media Library contains a thumbnail view along with a list view.

    Is there any solution out there that meets this criteria:

    • Not a plugin.
    • Restricts Authors specifically from seeing other media besides his/her own in the Media Library as well as in the Post Media modal popup.
    • Does not interfere with Advanced Custom Fields.
    • Also, would be great if the media count number corresponded correctly versus showing the total amount of files from all users.

    Thanks!

Viewing 1 replies (of 1 total)
  • Thread Starter polyfade

    (@polyfade)

    I’ve found this solution to somewhat work, but without the number of media items being relative:

    //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( 'edit_others_posts' ) ) {
    			//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('edit_others_posts') )
        $wp_query_obj->set('author', $current_user->ID );
        return;
    }

    For some reason, this works !current_user_can( 'edit_others_posts' ) but this does not, !current_user_can( 'manage_media_library' )

Viewing 1 replies (of 1 total)
  • The topic ‘Restrict Media access to authors with WP v4.x’ is closed to new replies.