且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Yoast SEO插件中删除操作

更新时间:2023-12-01 23:16:34

请参考 remove_action 文档:

  1. 您可能需要优先考虑将操作删除到添加操作之后发生的挂钩上.
  2. 您无法在添加操作之前成功删除该操作.
  3. 运行操作后,您也无法删除它.
  4. 要删除操作,优先级必须与最初添加的功能相匹配.
  1. You may need to prioritize the removal of the action to a hook that occurs after the action is added.
  2. You cannot successfully remove the action before it has been added.
  3. You also cannot remove an action after it has been run.
  4. To remove an action the priority must match the priority with with the function was originally added.

对于您而言,我相信其中一些问题(尤其是#3和#4)正在引起问题:

In your case, I believe several of these issues (especially #3 and #4) are causing problems:

首先,add_action上的优先级太高.通过将其设置为较高,它会在所有Yoast wp_head操作运行之后 运行.取而代之的是,挂接到要删除的同一操作上,但是编号很低(例如-99999),以使其在运行Yoast操作之前 运行. (此外,我已经分为两个功能,只是为了确保它们在正确的时间运行-每个动作一个-wp_headwpseo_head).

First, the priority on your add_action is too high. By setting it this high, it's running after all of the Yoast wp_head actions are run. Instead, hook into the same action you want to remove, but with a very low number, such as -99999, to cause it to run before the Yoast actions are run. (Further, I've broken into two functions, just to be sure they are run at the correct time - one for each action - wp_head and wpseo_head).

第二,您的优先级与Yoast代码中的优先级不匹配.我已经浏览了所有的Yoast代码以查找所有这些操作,并在下面的代码中进行了记录/更正-例如,我可以告诉您Yoast代码中的metakeywords钩子为11,因此您的remove_action(优先级为40)不管用.

Second, your priorities do not match the priorities in the Yoast code. I've dug through all the Yoast code to find all of these actions and documented / corrected in the code below - and I can tell you for example the metakeywords hook in Yoast code is 11, so your remove_action (with priority 40) will not work.

最后,Yoast将这些操作添加到$this(WPSEO_Frontend类的实例化版本),而不是类方法的静态版本.例如,这意味着remove_action无法基于函数数组(WPSEO_Frontendhead)找到它们.相反,您需要加载Yoast的实例化版本,并将那个传递给remove_action函数.

Finally, Yoast adds these actions to $this (an instantiated version of the WPSEO_Frontend class), not static version of the class methods. This means that remove_action is not able to find them based on the function array(WPSEO_Frontend, head), for example. Instead, you need to load the instantiated version of Yoast, and pass that in to the remove_action functions.

下面的记录代码:

// Remove ONLY the head actions.  Permits calling this at a "safe" time
function remove_head_actions() {
    // not Yoast, but WP default. Priority is 1
    remove_action( 'wp_head', '_wp_render_title_tag', 1 );

    // If the plugin isn't installed, don't run this!
    if ( ! is_callable( array( 'WPSEO_Frontend', 'get_instance' ) ) ) {
        return;
    }

    // Get the WPSEO_Frontend instantiated class
    $yoast = WPSEO_Frontend::get_instance();
    // removed your "test" action - no need
    // per Yoast code, this is priority 0
    remove_action( 'wp_head', array( $yoast, 'front_page_specific_init' ), 0 );
    // per Yoast code, this is priority 1
    remove_action( 'wp_head', array( $yoast, 'head' ), 1 );
}

function remove_wpseo_head_actions() {
    // If the Yoast plugin isn't installed, don't run this
    if ( ! is_callable( array( 'WPSEO_Frontend', 'get_instance' ) ) ) {
        return;
    }

    // Get the Yoast instantiated class
    $yoast = WPSEO_Frontend::get_instance();
    remove_action( 'wpseo_head', array( $yoast, 'head' ), 50 );
    // per Yoast code, this is priority 6
    remove_action( 'wpseo_head', array( $yoast, 'metadesc' ), 6 );
    // per Yoast code, this is priority 10
    remove_action( 'wpseo_head', array( $yoast, 'robots' ), 10 );
    // per Yoast code, this is priority 11
    remove_action( 'wpseo_head', array( $yoast, 'metakeywords' ), 11 );
    // per Yoast code, this is priority 20
    remove_action( 'wpseo_head', array( $yoast, 'canonical' ), 20 );
    // per Yoast code, this is priority 21
    remove_action( 'wpseo_head', array( $yoast, 'adjacent_rel_links' ), 21 );
    // per Yoast code, this is priority 22
    remove_action( 'wpseo_head', array( $yoast, 'publisher' ), 22 );
}

最后的注释:.

删除WPSEO_Frontend :: head动作非常费力.这将使您可能不希望删除的其他所有内容全部丢失.

Remove the WPSEO_Frontend::head action is very heavy handed. This will yank a whole host of other things you probably don't want removed.

第二,修改这些操作的输出而不是完全删除它们可能是更好的.

Second, It's probably better to modify the output of these actions, rather than removing them completely.

例如

add_action('wpseo_metakeywords', 'your_metakeywords_function');

function your_metakeywords_function( $keywords ) {
    // modify the keywords as desired
    return $keywords;
}