Tag Archives: privileges

By default, WordPress allows authors/users to see all images uploaded to a WordPress site’s media library. This could be a problem if your site has users with individual images/galleries. This WordPress snippet will allow you to restrict WordPress media library access to the user’s own uploads.

Instructions
Add this code to your WordPress theme function.php file.

// Limit access to media library (users can only see/select own media)
add_filter( 'ajax_query_attachments_args', 'wpsnippet_show_current_user_attachments' );
function wpsnippet_show_current_user_attachments( $query ) {
   $user_id = get_current_user_id();
   if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts')) {
      $query['author'] = $user_id;
   }
   return $query;
} 

Heres a little nifty WordPress snippet that will show the number of queries made and the page load time. This can come in handy if you, for instance, need to debug or optimize the performance of your WordPress site.

Instructions
Add this code snippet to a template file during testing in the location where you want the info to show. In this example the output is wrapped in an “if user is administrator” condition – so that the output is only visible to users with administrator rights.

<?php if(current_user_can('administrator')): ?>
     <?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.
<?php endif; ?>

Related links
http://www.livexp.net/wordpress/display-wordpress-page-loading-time-and-number-of-queries.html

Put this in the theme post-/page file to make an “Edit Page” or “Edit Post” link in your template file – that takes you into the admin page-/post edit mode. This link will only be visible when the viewer is logged in and has appropriate permissions to edit that page (administrator or author).
<?php edit_post_link(__('Edit This')); ?>

Related links:
https://codex.wordpress.org/Function_Reference/edit_post_link