banner



How Do I Modify Blog Approval Notices In Wordpress

Occasionally a plugin or theme will need to display a notice to users in the WordPress dashboard. This is fairly easy to do using the admin_notices hook, which shows a standard message box at the top of the screen.

Example Admin Notices

Display a Standard Notice

function my_admin_notice(){     echo '<div class="updated">        <p>I am a little yellow notice.</p>     </div>'; } add_action('admin_notices', 'my_admin_notice');        

Since this div is classed "updated" the notice will display yellow. If the class is changed to "error" it displays in red.

How To Make a Dismissible Notice

With a little more work it's also possible to display a notice stays present until the user clicks to ignore it. This could be a good way to ensure the user sees the message but also won't get annoyed by it.

The following example was adapted from the AddThis plugin. I also use something similar in the Options Framework.

If a user clicks to hide the notice, it will save their preference in the user meta.

/* Display a notice that can be dismissed */  add_action('admin_notices', 'example_admin_notice');  function example_admin_notice() { 	global $current_user ;         $user_id = $current_user->ID;         /* Check that the user hasn't already clicked to ignore the message */ 	if ( ! get_user_meta($user_id, 'example_ignore_notice') ) {         echo '<div class="updated"><p>';          printf(__('This is an annoying nag message.  Why do people make these? | <a href="%1$s">Hide Notice</a>'), '?example_nag_ignore=0');         echo "</p></div>"; 	} }  add_action('admin_init', 'example_nag_ignore');  function example_nag_ignore() { 	global $current_user;         $user_id = $current_user->ID;         /* If user clicks to ignore the notice, add that to their user meta */         if ( isset($_GET['example_nag_ignore']) && '0' == $_GET['example_nag_ignore'] ) {              add_user_meta($user_id, 'example_ignore_notice', 'true', true); 	} }        

Display Notices Only On Certain Admin Pages

If possible, target the notice to only appear on certain pages where the user needs to see them. You can do that by using the $pagenow global variable.

For example, this notice will only appear on the plugins page:

function my_admin_notice(){     global $pagenow;     if ( $pagenow == 'plugins.php' ) {          echo '<div class="updated">              <p>This notice only appears on the plugins page.</p>          </div>';     } } add_action('admin_notices', 'my_admin_notice');        

Check User Role Before Displaying a Notice

Notices should only be displayed to users that can actually do something about them. For instance, if the user cannot edit theme options, there's no point in displaying a notice about it.

Here's some common roles checks you might want to do:

if ( current_user_can( 'install_plugins' ) ) if ( current_user_can( 'manage_options' ) ) if ( current_user_can( 'edit_theme_options' ) )        

Notice Etiquette

Notices can be annoying, so be careful with how you use them. Keep the text short and try not display more than one. Use sparingly.

Other Resources

  • http://theme.it/how-to-display-an-admin-notice-for-required-theme-plugins
  • http://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices

How Do I Modify Blog Approval Notices In Wordpress

Source: https://wptheming.com/2011/08/admin-notices-in-wordpress/

Posted by: hardwickwhin1969.blogspot.com

0 Response to "How Do I Modify Blog Approval Notices In Wordpress"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel