在面向对象的设计中,使用WordPress核心类的最佳方式是什么?
我正在尝试使用$wp_admin_bar 删除几个默认的WordPress设计,但我找不到在哪里添加不会触发错误的$wp\\u admin\\u栏。
下面是我的代码The comments should help to understand what I have tried,以及我的思考过程是什么:
<?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\' );
}
}
在调用创建此类对象的类之前,我甚至尝试将其添加为全局。
if ( function_exists(\'add_action\') && class_exists(\'Init\')){
global $wp_admin_bar;
Init::register_services();
}
我遇到的错误是:
Fatal error: Uncaught Error: Call to a member function remove_menu() on null
我有一种感觉,我没有正确地调用$wp\\u admin\\u bar,但我也可能错误地知道错误的实际原因是什么。
我在这个网站上尝试了其他问题的解决方案,但到目前为止,所有问题都返回了相同的错误。