How to Add Date and Time to the Admin Bar in WordPress?

Wondering How to Add Date and Time to the Admin Bar in WordPress? Having the current date and time instantly visible in your WordPress admin bar can be extremely useful. In this post, I’ll guide you through how to easily add dynamic date and time output to the admin bar using a simple PHP code snippet.

Why Show Date/Time in the Admin Bar?

Here are some great reasons for displaying date/time text in the admin toolbar:

  • Quick at-a-glance reference for scheduling content, tracking site analytics, coordinating events and more
  • Saves you time from repeatedly checking date/time on your computer or mobile device
  • Handy visual indicator as you navigate between different admin pages
  • Provides helpful temporal context as you administer your site
  • Especially useful when editing scheduled posts, viewing update histories, analyzing traffic trends over time, etc.

Having constantly visible access to the current date and time can really improve your workflow and efficiency.

Step-By-Step Guide to Add the Date/Time Snippet

Adding in the functional PHP code requires just a few easy steps:

  1. Log into your WordPress dashboard
  2. Hover over “Appearance > Theme File Editor”
  3. Open your active theme’s functions.php file

Scroll down to the bottom of the file. Then paste in the following code block:

// Display Date & Time in Admin Bar
// Author: CodersVerse.com

/*
* Function to output the current date and time into the admin bar
* Runs on the 'admin_bar_menu' action hook to inject the date/time 
*/

function display_date_and_time_in_admin_bar() {

    // Get global admin bar object
    global $wp_admin_bar;   

    // Format current date and time string
    $current_time = date('Y-m-d H:i:s');  

    // Add a new admin bar root-level menu node
    $wp_admin_bar->add_menu( array(
      // Unique node ID
      'id'    => 'custom_date_time', 
      
      // Dynamic date/time title text
      'title' => $current_time,
       
      // Set to false to avoid linking anywhere
      'href'  => false,
    ));

}

// Hook our function into 'admin_bar_menu' to display the date/time  
add_action( 'admin_bar_menu', 'display_date_and_time_in_admin_bar' );

The key points:

  • date() formats the date and time string
  • Output gets added as a new admin bar node
  • We hook our function into the admin_bar_menu action

Now click “Update File” and your date/time will instantly appear in the toolbar!

Customizations & Enhancements

Some ideas for improving and extending the functionality:

  • Add seconds, change date/time format
  • Link to blog page or site analytics dashboard
  • Output current online users as well
  • Create shortcode to display on frontend too
  • Conditionally show only for admin user roles
  • Use JavaScript for auto-updating every minute

The possibilities are endless! Displaying dynamic data like this can really boost your productivity.

Leave a Comment