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