WordPress.org

Ready to get started?Download WordPress

Forums

Can admin_bar_menu or another action be used to modify the Dashboard admin bar? (7 posts)

  1. Eric
    Member
    Posted 1 year ago #

    I have used add_action('admin_bar_menu', 'modify_admin_bar') to change the admin bar on the "front end" of WordPress (the site the user sees). However, it seems that modify_admin_bar() is not called before the admin bar is rendered on the "back end" (the Dashboard or admin side of WordPress).

    How can I modify the admin bar as it is presented on the admin side of WordPress?

    Here's an example...

    add_action( 'admin_bar_menu', 'modify_admin_bar');
    function modify_admin_bar() {
        error_log('admin barring');
    }

    In this example, my error log shows "admin barring" every time I refresh a page on the user side of WordPress, but never shows "admin barring" when I refresh the admin side (the Dashboard) of WordPress. It does not seem that the function gets called from the admin side. I am running WP 3.5, but have confirmed the same behavior in 3.4.2.

  2. halferdev
    Member
    Posted 1 year ago #

    Seems to work for me (stripped out of some of my current work in progress, so sorry if I've missed a bit of code):

    class CommentsEncryptMain
    {
    	public function __construct()
    	{
    		$this->initScreens()
    	}
    
    	protected function initScreens()
    	{
    		// Set up handler for admin screen hook
    		add_action('admin_menu', array($this, 'screensHandler'));
    
    		// Set up handler for admin bar registration (100 = put menu item at the end of standard items)
    		add_action('admin_bar_menu', array($this, 'adminBarRegister'), 100);
    	}
    
    	public function adminBarRegister(WP_Admin_Bar $WpAdminBar)
    	{
    		$privKeySet = (bool) $_COOKIE[self::COOKIE_PRIV_KEY];
    
    		$WpAdminBar->add_menu(
    			array(
    				'id' => 'encdemo_key_status',
    				'title' => $privKeySet? 'Private key set' : 'Private key unknown',
    				'href' => 'edit-comments.php?page=xyz',
    			)
    		);
    	}
    }
    
    new CommentsEncryptMain();

    If that doesn't help, can you confirm that your plugin is actually activated, and is called for admin screens?

  3. Eric
    Member
    Posted 1 year ago #

    @halferdev, you mention "and is called for admin screens." How do I ensure a plugin is called for admin screens?

    I know my plugin is actually getting activated because I do get the proper entries in the error log when I reload user screens. But I'm getting nothing when admin screens load.

  4. halferdev
    Member
    Posted 1 year ago #

    Well, I dunno about the proper way, but if this is your plugin PHP file:

    <?php
    // Put this at the start of your plugin file
    echo "I got called!\n";

    Inelegant, but it works ;)

  5. Eric
    Member
    Posted 1 year ago #

    That's what the call to error_log() is, just a bit less intrusive. :)

    I can say for sure, my plugin is ONLY getting called for user screens. Nothing in my plugin gets executed when I'm on an admin screen.

    I wonder why?

  6. Eric
    Member
    Posted 1 year ago #

    Gosh, blush, I figured it out.

    error_log() on my system was writing to two different logs. One log for the user page, another log for the admin page.

    The plugin was working fine, I just could not see the log entries because I was looking at the wrong long.

    Sorry to bother you all! @halferdev, I appreciate your help. Your "echo" clue finally helped me see the problem.

  7. halferdev
    Member
    Posted 1 year ago #

    Ha ha, no worries! Glad you figured it out.

Topic Closed

This topic has been closed to new replies.

About this Topic