[英]How can I change action priority in Wordpress?
我正在使用Thematic框架作为儿童主题。 它有许多钩子,但我特别关注thematic_header()。 thematic_header()钩子添加了以下操作(通过add_action):
<?php
add_action('thematic_header', 'thematic_brandingopen', 1);
add_action('thematic_header', 'thematic_blogtitle', 3);
add_action('thematic_header', 'thematic_blogdescription', 5);
add_action('thematic_header', 'thematic_brandingclose', 7);
add_action('thematic_header', 'thematic_access', 9);
?>
行动的内容无关紧要。
我的问题是:如何改变有关五项行动的优先次序。 例如,我想在thematic_brandingopen()之前加载thematic_access()。 我能够弄清楚的唯一方法就是删除并重新添加动作,ala:
<?php
function remove_thematic_actions() {
remove_action('thematic_header', 'thematic_access');
add_action('thematic_header', 'thematic_access', 0); //puts it above thematic_brandingopen
}
add_action ('init', 'remove_thematic_actions');
这似乎是一种非常简单的完成事情的愚蠢方式。 有没有办法访问和排序/重新排序任何存储WP中的操作的数据结构?
如果使用默认值10以外的优先级注册了挂钩,则还必须在remove_action()调用中指定优先级。
所以我认为你可以先删除使用以下内容
remove_action('thematic_header', 'thematic_brandingopen', 1);
remove_action('thematic_header', 'thematic_access', 9);
并使用不同的priority
再次添加
add_action('thematic_header', 'thematic_access', 1);
add_action('thematic_header', 'thematic_brandingopen', 2);
不要自我推销,但我已经做了一些工作,通过名为Prioritize Hooks的WordPress插件提供非编码解决方案。 我的插件允许您通过UI设置各种已注册挂钩的优先级,并在运行时覆盖,以便不修改代码。
以防这有助于某人,变量动作存储在is中
global $wp_filter;
var_dump( $wp_filter[$hook_name] );
这是一个数组数组,其中键是添加操作时的优先级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.