Ask your WordPress questions! Pay money and get answers fast! (more info)

Need code in functions.php to limit media library by user WordPress

  • SOLVED

Currently I have the following code in my functions.php file:


add_filter( 'posts_where', 'devplus_attachments_wpquery_where' );
function devplus_attachments_wpquery_where( $where ){
global $current_user;

if( is_user_logged_in() ){
// we spreken over een ingelogde user
if( isset( $_POST['action'] ) ){
// library query
if( $_POST['action'] == 'query-attachments' ){
$where .= ' AND post_author='.$current_user->data->ID;
}
}
}

return $where;
}


This code only shows items in the media library that a certain user has uploaded themselves.

I need to modify the above code so that it allows certain roles to see all media. Or if easier, only have the above code apply to a certain role.

Basically I want some users to see all media.

Answers (2)

2014-12-13

Dbranes answers:

Hi, please try the following:

<strong>Restriction by capability:</strong>

/**
* Restrict media views to a given user capability
*
* @see http://www.wpquestions.com/question/showChronoLoggedIn/id/10265
*
* Based on ideas from http://wordpress.stackexchange.com/q/1482/26350
*/

is_admin() && add_action( 'pre_get_posts', 'wpq_restrict_media_by_capability' );

function wpq_restrict_media_by_capability( $query ) {

$capability_to_view_all = 'manage_options'; // <-- Edit to your needs!

if( ( isset( $GLOBALS['pagenow'] ) && 'upload.php' === $GLOBALS['pagenow']
|| 'query-attachments' === filter_input( INPUT_POST, 'action' ) )
&& ! current_user_can( $capability_to_view_all )
) {
$query->set( 'author', get_current_user_id() );
}
}


<strong>Restriction by roles:</strong>

/**
* Restrict media views to user roles
*
* @see http://www.wpquestions.com/question/showChronoLoggedIn/id/10265
*
* Based on ideas from http://wordpress.stackexchange.com/q/1482/26350
*/

is_admin() && add_action( 'pre_get_posts', 'wpq_restrict_media_by_roles' );

function wpq_restrict_media_by_roles( $query ) {

$roles_to_view_media = array( 'editor', 'administrator' ); //<-- Edit this to your needs!

if( ( isset( $GLOBALS['pagenow'] ) && 'upload.php' === $GLOBALS['pagenow']
|| 'query-attachments' === filter_input( INPUT_POST, 'action' ) )
&& ( $u = wp_get_current_user() ) instanceof WP_User
&& count( array_intersect( $roles_to_view_media, $u->roles ) ) == 0
) {
$query->set( 'author', get_current_user_id() );
}
}


Kyler Boudreau comments:

Thanks to both of you.

Dbranes - tried both ways and your code worked great. Quick question - If I don't want to just specify one, but two roles, how would the above code change? Voting you the cash.

2014-12-13

Kun MMO answers:

Try test with this code

add_action( 'admin_menu', 'custom_remove_media_menu_pages', 999 );
function custom_remove_media_menu_pages() {
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
return; // Not logged in.
}
$role_manager_media = array ('Administrator', 'Editor'); //edit role can manager media page
$current_user_id = $current_user->ID;
$user = new WP_User( $current_user_id );
$allow_access_media = false;
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role ) {
if( in_array($role, $role_manager_media) ) {
$allow_access_media = true;
break;
}
}
}
if(!$allow_access_media) {
remove_menu_page('upload.php'); // remove Media menu from admin left menu

//remove_submenu_page( 'upload.php' ,'upload.php' );
//remove_submenu_page( 'upload.php' ,'media-new.php' );
}

}