Extend Wordpress Core Classes in OOP Theme?

What is the best way of using WordPress's core classes in the context of an object-oriented design?

I am trying to use $wp_admin_bar to remove a couple of the default WordPress designs but I am not able to find where to add $wp_admin_bar that does not trigger an error.

Below is my code. The comments should help to understand what I have tried, and what my thought process was:

?php

    defined( 'ABSPATH' ) or die('Nothing to see here');

// Since wp_admin_bar is a class I probably need to extend to it but I am unsure what 
//file needs to be required to achieve this
class adminPanel {

  //I tried adding it here
  //protected $wp_admin_bar;
  //I also tried public $wp_admin_bar

  //resgister() function is treated as something like a __construct and is called by a 
  //Init class so $wp_admin_bar can't be declared global before the class is 
  //instantiated
  public function register(){

      //Remove wordpress logo from top nav
      add_action( 'wp_before_admin_bar_render', array( $this, 'remove_wp_logo' ), 0 );


  }

  function remove_wp_logo() {
      //As pointed out in one of the answers global $wp_admin_bar; might 
      //be added here but this causes: Parse error: syntax error, 
      //unexpected 'global' (T_GLOBAL)  
      $wp_admin_bar-remove_menu( 'wp-logo' );
  }

}

I even tried to add it as a global before the class that creates an object of this class is called.

if ( function_exists('add_action')  class_exists('Init')){
   global $wp_admin_bar;
   Init::register_services();
}

The error I am getting is:

Fatal error: Uncaught Error: Call to a member function remove_menu() on null

I have a feeling that I am not calling $wp_admin_bar correctly but I could also by wrong about what the actual cause of the error is.

I have tried other solutions from other questions on this site but everything so far is returning the same error.

Topic development-strategy oop wp-admin theme-development customization Wordpress

Category Web


As Sally pointed out, you can use the global variable. However, that makes your code a bit harder to test and to understand. The better way is using the proper hook admin_bar_menu. Like this:

add_action( 'admin_bar_menu', function( \WP_Admin_Bar $wp_admin_bar ) {
    new adminPanel( $wp_admin_bar );    
});

And then take this object in your constructor:

class adminPanel 
{
    private $admin_bar;
    
    public function __construct( \WP_Admin_Bar $wp_admin_bar )
    {
        $this-admin_bar = $wp_admin_bar;
    }
}

Then you can access this object in all your methods, and you can mock it in your unit tests.


I am trying to use $wp_admin_bar to remove a couple of the default WordPress designs but I am not able to find where to add $wp_admin_bar that does not trigger an error.

The wp_before_admin_bar_render hook doesn't pass the admin bar object, but you can access it manually (from within a function or a class method) using global like so, just like the Codex example here:

function remove_wp_logo() {
    global $wp_admin_bar;

    $wp_admin_bar->remove_menu( 'wp-logo' );
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.