Add a notice to users upon first login to the admin area

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

Leave a Comment