OOP和WordPress插件主题支持

时间:2015-10-29 10:37:20

标签: php wordpress oop wordpress-plugin

我试图使用OOP创建WordPress插件,但我在开始时遇到困难。我想添加通过WordPress面板修改页眉的功能。

用户应该能够更改标题图像和文本。我发现add_theme_support函数肯定会有用。

最初我尝试在WordPress面板的Apperance菜单中创建标题部分,所以我尝试了这段代码:

class Plugin{
    public function __construct(){
        if (is_admin()) {
           add_action('admin_init', array($this,'admin_init'));
        }
    }
    public function admin_init(){
        add_action( 'after_setup_theme', array($this,'customHeader') );
    }
    public function customHeader(){
        add_theme_support('custom-header');
    }
}
$plugin=new Plugin();

不幸的是,有些事情是错误的,并没有给出任何结果。页眉编辑页面未显示在管理面板中。

你知道它为什么不起作用,哪里出错了?

1 个答案:

答案 0 :(得分:0)

您正在添加' after_setup_theme' at' admin_init'但这需要提前设定。

class Plugin {
    public function __construct(){
         add_action( 'after_setup_theme', array($this,'customHeader') );
    }

    public function customHeader(){
        add_theme_support('custom-header');
    }
}

$plugin=new Plugin();

当您在管理区域时,您不会只想添加主题支持。