3

I'd like to give users a little welcome message on their first login to the admin area. Once they hide the message it should not appear again. I noticed admin_notice is a hook but don't know where to start. Thanks.

2 Answers 2

5

something like:

add_action('admin_notices', 'my_notice');    
add_action('wp_ajax_hide_my_notice', 'hide_my_notice');

function hide_my_notice(){
  check_ajax_referer('hide-my-notice');
  $user = wp_get_current_user();

  // update status for this user
  $seen_notice = get_option('my_notice');
  $seen_notice[$user->ID] = true;
  update_option('my_notice', $seen_notice);
  exit;
} 

function my_notice(){

  $user = wp_get_current_user();
  $seen_notice = get_option('my_notice');

  // already seen it?
  if(isset($seen_notice[$user->ID]) && $seen_notice[$user->ID])
    return;

  ?>
  <div class="updated fade below-h2">
    <p>
      Hi <?php print esc_attr($user->user_login); ?>! Duuuuuude, whatz upp???
      <a class="hide-me"> X </a>
    </p>
   </div>  

   <script type="text/javascript">    

     // this should go in a javascript file;
     // use wp_localize_script() to send variables from PHP to it
     jQuery(document).ready(function($) {

       $('a.hide-me').on('click', function(){

         $.ajax({
           url: '<?php print admin_url("admin-ajax.php"); ?>',
           type: 'GET',
           context: this,
           data: ({
             action: 'hide_my_notice',
             _ajax_nonce: '<?php print wp_create_nonce('hide-my-notice'); ?>'
           }),
           success: function(response){ 
             $(this).closest('div').remove();
           }    
         });
       });

     });

   </script>    
  <?php     
}

You can also use options cookies or transients to store the notice status

9
  • Nice, but why the obscure variable? $$$ (or is there more to it then that?)..
    – t31os
    Commented Dec 20, 2010 at 10:43
  • no, it's just a variable like any other :) Commented Dec 20, 2010 at 16:59
  • Great stuff. Just one more question is how to get it to appear under the H2 like other notices. It has the class below-h2 but doesn't appear there. Thanks. Commented Dec 21, 2010 at 8:36
  • does it work if you remove the "below-h2" class? Commented Dec 21, 2010 at 10:17
  • Hmm, it appears the notice doesn't get hidden when i come back to the page :P Does it work for anyone else? How might i go about using the other two methods for hiding it? Sorry i'm in experinced. Commented Dec 21, 2010 at 12:03
2

WordPress 3.3 has Pointers for this.

Example of a pointer

WordPress 3.3 is planned for November [2011] and we published the scope a while ago here on Theme.fm. As mentioned in that post, 3.3 will get a series of UI improvements, one of which is Pointers (or Admin Pointers). We learned about pointers in WordPress a few hours ago from one of Daryl Koopersmith’s changesets so we decided to give it a spin ourselves.

What are Admin Pointers?

Here’s what Jane wrote in the trac ticket for WordPress pointers:

When a new user-facing feature is included in a core update, display a new feature pointer that highlights the new feature a la Facebook, Gmail, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.