Created
February 10, 2016 22:23
-
-
Save fastmover/d028b6984ea946f221d5 to your computer and use it in GitHub Desktop.
WordPress: Allow the user to only see media they've uploaded.
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
| add_action('pre_get_posts','users_own_attachments'); | |
| function users_own_attachments( $wp_query_obj ) { | |
| global $current_user, $pagenow; | |
| if( !is_a( $current_user, 'WP_User') ) | |
| return; | |
| if( !in_array( $pagenow, array( 'upload.php', 'admin-ajax.php' ) ) ) | |
| return; | |
| if( ! current_user_can ( 'administrator' ) && ! is_admin () ) | |
| $wp_query_obj->set('author', $current_user->ID ); | |
| return; | |
| } | |
| add_filter( 'ajax_query_attachments_args', 'show_current_user_attachments', 10, 1 ); | |
| function show_current_user_attachments( $query = array() ) { | |
| $user_id = get_current_user_id(); | |
| if( $user_id ) { | |
| $query['author'] = $user_id; | |
| } | |
| return $query; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment