我试图在我试图构建的插件中使用上述两个函数,但显然它们不起作用,因为插件是在页面准备就绪之前加载的,在帖子加载之前加载的,以了解帖子的类型。
也就是说,我该如何解决这个问题?我只是想做一些简单的事情,比如:
if ( is_single() && get_post_type() == \'boxy\' ) {
$setting = 200;
}
但由于上述原因,这一说法永远不会成为事实。谢谢你的帮助!我试图在我试图构建的插件中使用上述两个函数,但显然它们不起作用,因为插件是在页面准备就绪之前加载的,在帖子加载之前加载的,以了解帖子的类型。
也就是说,我该如何解决这个问题?我只是想做一些简单的事情,比如:
if ( is_single() && get_post_type() == \'boxy\' ) {
$setting = 200;
}
但由于上述原因,这一说法永远不会成为事实。谢谢你的帮助!您需要挂接到WordPress加载序列中的适当位置。
例如
add_action(\'init\', \'my_function\'); //is too early and won\'t work!
其中,add_action(\'wp\', \'my_function\'); //will work!
我也会,add_action(\'template_redirect\', \'my_function\'); //this works too!
事实上template_redirect
在将页面/帖子呈现给查看器之前激发,以便它是执行操作的适当位置。以下面的示例为例,如果将其放置在插件文件中,将成功通过is_single
条件检查,然后继续添加筛选器,该筛选器将替换the_content
对于完全自定义的结果,在本例中是一个输出new content
将显示在数据库中保存的内容的位置。
function plugin_function(){
if(is_single()) {
add_filter(\'the_content\', \'filter_content\');
function filter_content(){
echo \'new content\';
}
}
}
add_action(\'template_redirect\', \'plugin_function\');
例如,尝试将上面的最后一行改为,add_action(\'init\', \'plugin_function\');
使用init
钩子是到早期的,而不是像上面的函数那样看到我们过滤的内容,你会注意到数据库中的常规帖子内容被显示出来了。希望这有帮助。
add_action/add_filter
?Example:
add_action( \'template_redirect\', array( \'YourClassNmae\', \'init\' ) );
class YourClassNmae {
public static function init() {
$class = __CLASS__;
new $class;
}
public function __construct() {
//your actions/filters are to be added here...
add_filter(\'the_content\', array( $this, \'filter_content\') );
}
public function filter_content(){
echo \'new content\';
}
}
Codex Reference: Using add_action with a class插件更新和wordpress更新是如何工作的?如果我修改了一个插件的代码,过了一段时间,我会用wordpress和这些插件的最新更新来更新它,那么它会有什么影响?