wordpress: actions, filters & hooks

2019-08-14 18:50发布

问题:

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,

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 ...

that line in post-template.php specifies:

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

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 );
}

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


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...?" !

回答1:

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

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.

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 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);

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);

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.

Update

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.

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