简体   繁体   English

如何在Wordpress中更改操作优先级?

[英]How can I change action priority in Wordpress?

I'm using the Thematic framework for a child theme. 我正在使用Thematic框架作为儿童主题。 It has a number of hooks, but I'm looking at thematic_header() in particular. 它有许多钩子,但我特别关注thematic_header()。 The thematic_header() hook adds the following actions (through add_action): 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);
?>

The content of the actions is irrelevant. 行动的内容无关紧要。

My question is this: How can I change the priorities of the five actions in question. 我的问题是:如何改变有关五项行动的优先次序。 For instance, I want thematic_access() to load before thematic_brandingopen(). 例如,我想在thematic_brandingopen()之前加载thematic_access()。 Only way to do this that I've been able to figure out is by removing and re-adding the actions, ala: 我能够弄清楚的唯一方法就是删除并重新添加动作,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');

That seems like a stupid way of accomplishing something very simple. 这似乎是一种非常简单的完成事情的愚蠢方式。 Is there a way to access and sort/reorder whatever data structure stores actions in WP? 有没有办法访问和排序/重新排序任何存储WP中的操作的数据结构?

From WordPress 来自WordPress

if a hook was registered using a priority other than the default of 10, then you must also specify the priority in the call to remove_action(). 如果使用默认值10以外的优先级注册了挂钩,则还必须在remove_action()调用中指定优先级。

So I think you can first remove using following 所以我认为你可以先删除使用以下内容

remove_action('thematic_header', 'thematic_brandingopen', 1);
remove_action('thematic_header', 'thematic_access', 9);

and the add again using different priority 并使用不同的priority再次添加

add_action('thematic_header', 'thematic_access', 1);
add_action('thematic_header', 'thematic_brandingopen', 2);

not to self-promote but I have done some work on this to provide a non-coding solution through a WordPress plugin called Prioritize Hooks . 不要自我推销,但我已经做了一些工作,通过名为Prioritize Hooks的WordPress插件提供非编码解决方案。 My plugin lets you set the priorities of various registered hooks through a UI and does the overriding in runtime so the code isn't modified. 我的插件允许您通过UI设置各种已注册挂钩的优先级,并在运行时覆盖,以便不修改代码。

Just in case this helps someone, the variable actions are stored in is 以防这有助于某人,变量动作存储在is中

global $wp_filter;
var_dump( $wp_filter[$hook_name] );

Which is an array of arrays with the keys being the priority when the action was added. 这是一个数组数组,其中键是添加​​操作时的优先级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM