我有以下在中实例化的类function.php
文件在构造器中,我为主题设置了激活和停用挂钩。当我真的在这个主题之间切换,比如说十四岁,然后再回来的时候,这两个词似乎都不叫了。由于不调用echo语句:
namespace Core;
class Activation {
protected $themeOptions = null;
public function __construct(){
if ($this->themeOptions === null) {
$this->themeOptions = \\Freya\\Factory\\Pattern::create(\'Freya\\Templates\\Options\');
}
if(is_admin()) {
echo \'Sample\';
// On Activation ...
add_action(\'after_theme_switch\', array($this, \'themeActivation\'));
// On Deactivation ...
add_action(\'switch_theme\', array($this, \'themeDeactivation\'));
}
}
public function themeActivation(){
echo \'activated\';
$this->setUpPluginOptions();
$this->installPlugins();
}
public function themeDeactivation(){
echo \'deactivated\';
$this->themeOptions->deleteOptions(array (
\'theme_options\',
\'plugin_management\'
));
}
public function plugin_install_error_message() {
$this->themeOptions->renderView(\'plugin_install_error_message\');
}
protected function installPlugins() {
if (!get_option(\'plugin_installed\') && !get_option(\'plugin_install_error\') && is_admin()) {
$pluginManagement = new \\Core\\Plugins\\InstallPlugins();
$pluginManagement->installPlugins();
if (get_option(\'plugin_install_error\')) {
add_action(\'admin_notices\', array ($this, \'plugin_install_error_message\'));
}
}
}
protected function setUpPluginOptions() {
$this->themeOptions->createOptions(
array (
\'plugin_management\' => array (
\'plugin_installed\',
\'plugin_install_error\',
\'plugin_install_success\',
)
)
);
}
}
The
echo Sample;
在激活主题时调用。所以我知道它至少到了这里。但另一个
echo
不调用。
有人能告诉我为什么add_action
不是在做我想做的吗?我必须打电话吗do_action
? is there something I am missing?
最合适的回答,由SO网友:cybmeta 整理而成
您正在使用after_theme_switch
操作,但正确名称为after_switch_theme
.
此外,您不会在上看到echo语句after_theme_switch
和switch_theme
. 例如,要调试可以使用的挂钩中的内容,error_log()
函数并在PHP错误日志文件中查找消息(您需要启用错误“On”和/或WP\\U调试)。
我已经对此进行了测试,效果良好:
new Activation;
class Activation {
public function __construct(){
add_action(\'after_switch_theme\', array($this, \'themeActivation\'));
add_action(\'switch_theme\', array($this, \'themeDeactivation\'));
}
public function themeActivation(){
error_log( \'activated\' );
}
public function themeDeactivation(){
error_log( \'deactivated\' );
}
}