WordPress:动作,过滤器和钩子 [英] wordpress: actions, filters & hooks

查看:113
本文介绍了WordPress:动作,过滤器和钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此很陌生(了解WP Guts),我想更好地了解钩子和过滤器,我无法从Codex那里得到它。

I'm quite new to this (understanding the WP Guts), and I wanted to understand the Hooks and Filters better, I can't get it right from Codex.

我做了一个简单的测试,

I did a simple test,

这个想法是重写get_title()方法,以便从页面上删除标题中的 Protected:语句受保护,有一个protected_title_format过滤器,我想使用它...

the idea is to override the get_title() method in order to erase the "Protected: " sentence from the title if the page is protected, there is a protected_title_format filter, and I thought using it ...

post-template.php 中的行指定:

$protected_title_format = apply_filters('protected_title_format', __('Protected: %s'));

要从CODEX中获得什么,我需要删除该过滤器并添加自己的过滤器,例如

for what I could get from CODEX, I need to remove that filter and add my own, like

remove_action('protected_title_format');
apply_filters('protected_title_format', __('MY OWN PAGE Protected: %s'));

使用,当然,像这样的东西

using, off course something like

// Removing action
function remove_title_action() {
    remove_action('protected_title_format','get_the_title',3);
}
add_action('init','remove_title_action');

// Adding custom function
add_action('protected_title_format','fancy_title', 3, 4);

function fancy_title($id = 0) {
    $post = &get_post($id);
    $title = $post->post_title;

    echo "I'm the king of the world!... >" . $title . "< & >" . $post . "<";

    if ( !is_admin() ) {
    if ( !empty($post->post_password) ) {
        $protected_title_format = apply_filters('protected_title_format', __('MY OWN PAGE Protected: %s'));
        $title = sprintf($protected_title_format, $title);
    }
    }
    return apply_filters( 'the_title', $title, $post->ID );
}

我可以得到输出的回显,但是我没有得到$ id(并且为此,没有$ title或$ post),此方法是get_title()的副本,剥离了除了受保护的部分字符串以外的所有内容。

I can get the echo to output, but I dont get the $id (and for that, no $title or $post), this method is a copy of get_title() stripping out everything but the protected part string.

任何人都可以解释一下吗?我这是怎么工作的
谢谢

Can anyone care to explain me how this works? Thank you

PS 我想学习,这是这个问题,不是有人告诉我嘿,只需转到post-template.php并进行更改,因为那样的话我会问更新WP ...怎么样?。 !

P.S. I want to learn, this is the idea of this question, not someone to tell me "Hey, just go to post-template.php and change it", because then I would ask "How about when I update WP...?" !

推荐答案

实际上,这比您尝试的要简单得多。不过,您在正确的轨道上。

You can actually do this much more simply than what you're trying. You're on the right track though.

基本上,您要做的是创建自己的函数,该函数将删除WordPress标题的 Protected:部分。最简单的方法是创建一个函数,该函数使用preg_replace()搜索 Protected:文本并将其剥离。您也可以轻松地用它自己的文本自动替换字符串。

Basically, what you want to do is create your own function that will strip out the "Protected: " part of the WordPress titles. The easiest way to do this is to simply create a function that uses preg_replace() to search for the "Protected: " text and strip it. You could just as easily have it auto-replace the string with your own text.

这是一个示例函数。我们将$ title作为参数并返回其修改后的版本。

Here's a sample function that does that. We're taking the $title as a parameter and returning the modified version of it.

function remove_protected_text($title) {
  $match = '/Protected: /';
  $replacement = '';

  $title = preg_replace($match, $replacement, $title);
  return $title;
}

接下来要做的实际上是将函数添加到过滤器挂钩中。在这种情况下,我们感兴趣的过滤器挂钩是 the_title。因此,我们在刚编写的函数下方添加以下行:

The next thing we want to do is actually add our function to a filter hook. The filter hook that we're interested in in this cases is 'the_title'. So, we add the following line below the function we just wrote:

add_filter( 'the_title', 'remove_protected_text', 10);

这会将我们的函数 remove_protected_text()添加到 the_title过滤器。在这种情况下,我使用了第三个参数来赋予过滤器优先级10。这是完全可选的,但我认为此过滤器的优先级很低。

This adds our function remove_protected_text() to the 'the_title' filter. In this case I've used the third argument to give our filter a priority of 10. This is totally optional, but I figure this filter is a pretty low priority.

因此,我们的代码合起来应该像这样:

So all together our code should look like this:

function remove_protected_text($title) {
    $match = '/Protected: /';
    $replacement = '';

    $title = preg_replace($match, $replacement, $title);
    return $title;
}
add_filter( 'the_title', 'remove_protected_text', 10);

将该代码添加到主题中的functions.php文件中将使其起作用。您可以为WordPress的大多数输出​​文本的部分编写这样的过滤器。

Adding that code to the functions.php file in your theme will allow it to work. You can write filters like this for most of the parts of WordPress that output text.

更新

此处是该函数的修订版,应获取转换后的字符串 Protected:并将其删除:

Here's a revised version of the function that should get the translated string of "Protected: " and remove it:

function remove_protected_text($title) {
    $protected = __('Protected: %s');
    $protected = preg_replace('/ %s/', '', $protected);

    $match = "/${protected}/";
    $replacement = '';

    $title = preg_replace($match, $replacement, $title);

    return $title;
}
add_filter( 'the_title', 'remove_protected_text');

基本上,这里唯一的变化是我们使用__()函数来转换受保护的字符串,并然后去掉多余的位。这是一种小技巧,我敢肯定有更好的方法可以做到这一点,但是它确实可以在我的测试中使用。

Basically the only change here is that we are using the __() function to translate the protected string and then striping out the extra bits. This is kind of hackish, and I'm sure there's a better way to do it, but it does work in my testing.

我在西班牙语版本中进行了测试WordPress,它有效,所以让我知道它是否适用于您的项目。

I tested this out on a Spanish version of WordPress and it worked, so let me know if it works for your project.

这篇关于WordPress:动作,过滤器和钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆