Created
November 17, 2014 17:25
-
-
Save alanyoshida/fbe083f491e3769c6cdb to your computer and use it in GitHub Desktop.
Wordpress Snippet - Manage Your Media Only
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Manage Your Media Only | |
Description: Show only the author's Media in the edit list | |
Version: 0.1 | |
License: GPL | |
*/ | |
//Manage Your Media Only | |
function mymo_parse_query_useronly( $wp_query ) { | |
if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false ) { | |
if ( !current_user_can( 'update_core' ) ) { | |
global $current_user; | |
$wp_query->set( 'author', $current_user->id ); | |
} | |
} | |
} | |
add_filter('parse_query', 'mymo_parse_query_useronly' ); | |
// //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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment