Support

Account

Forum Replies Created

  • The if ( ! current_user_can( 'level_5' ) ) { conditional will prevent this from affecting administrators. For other users everything they will do is on the front end, but if they did have admin access it would be good to limit the images there as well.

    Thanks

  • I created a custom user role with capabilities similar to ‘author’. The code you posted limited the images visible in the Gallery field itself but not in the media library. I was able to get it working with the following:

    function restrict_media_library_to_current_user( $wp_query_obj ) {
    
    	if ( ! current_user_can( 'level_5' ) ) {
    
    		global $current_user, $pagenow;
    
    		if (  ! is_a( $current_user, 'WP_User' ) || 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' ) {
    			return;
    		}
    
    		$wp_query_obj->set( 'author', $current_user->ID );
    
    		return;
    	}
    }
    
    add_action( 'pre_get_posts', 'restrict_media_library_to_current_user' );
Viewing 2 posts - 1 through 2 (of 2 total)