Tag Archives: current_user_can()

This snippet hides the WordPress version update message that is displayed when there is a new version of WordPress available.

Instructions
Ad this to your functions.php file.

if ( !current_user_can('administrator') ) {
    add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
    add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
}

*note – in order for not to hide the update-availability for administrators, we have added the conditional tag: current_user_can() but let’s hide the message for everybody but the admins.

With this snippet, you can create a WordPress page template with restricted access only to those users with the right role to access to it.

Instructions
You will need to create the include, in this case error.php – this include should contain the message you wish to show.
If your template-custom.php page, is not intended for a given user, then your error.php could say something like: “Sorry, you are not allowed to access this page!”. Or you could do a redirect in your error.php file.

If you create custom roles you can do the same ie. buyers, clients, etc.

Ad this to the top section of your template-custom-php file.

<?php
/* Template Name: Restricted to Authors only */
     if ( !current_user_can('author')) {
	include('error.php');
	exit(0);
     }
?>

Related Links:
http://codex.wordpress.org/Roles_and_Capabilities

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