0

I'm wanting to apply a filter to everything.

add_filter('the_content', word_swap);

Is there a term I can replace the_content with that'll target all functions?

  • Ehm, "all" as in "several hundreds of functions regardless of what they do and what their filter is meant for"? – cjbj Jun 28 '16 at 13:48
  • What's the problem you're trying to solve here ? – birgire Jun 28 '16 at 14:13
  • Sitewide search and replace. Can't just change the text in the DB because it'll only be for certain users. – ditto Jun 28 '16 at 14:16
  • Yes, it's called all. But using it will very likely hurt your site's performance. – fuxia Jun 28 '16 at 19:18
  • So add_filter('all', ...) will work? 0.o – ditto Jun 28 '16 at 21:06
0

There is no "filter everything" function.

WordPress does something with their capital_P_dangit() function that attaches to 'the_title', 'the_content', and 'comment_text'. https://developer.wordpress.org/reference/functions/capital_p_dangit/

0

Take a look at kfriend's solution here...it works. Not sure if it carries any other risks but it does work.

https://stackoverflow.com/questions/772510/wordpress-filter-to-modify-final-html-output

You can pretty much replace anything with anything else, using this.

0

There's predefined filter called "all", you can use like add_filter('all', 'word_swap');

The function word_swap you pass as callback, will accept the filter tag (filter name) as first argument, and the second argument will be the value. The value argument can be skipped for some filters like "shutdown", so it must hold a default value.

function word_swap($tag, $value = null){
    // change value the way you want here
    return $changed_value;
}

Usage example 1: To restrict your function word_swap being executed for some filter tags, you can hold an array of tags for exclusion.

function get_tags_to_exclude(){
    return ['some_filter_to_exclude_1', 'some_filter_to_exclude_2'];
}
function word_swap($tag, $value = null){
    if( in_array( $tag, get_tags_to_exclude() ) ){
        return $value;
    }
    // do whatever you want here
    return changed_value;
}

Usage example 2: To allow your function word_swap to be executed for only some filter tags, you can hold an array of tags you allow.

function get_tags_to_allow(){
    return ['some_filter_to_allow_1', 'some_filter_to_allow_2'];
}
function word_swap($tag, $value = null){
    if( !in_array( $tag, get_tags_to_allow() ) ){
        return $value;
    }
    // do whatever you want here
    return changed_value;
}

It you want your filter to be called only for few cases then you'd better use add_filter one by one like

add_filter('tag1', 'word_swap');
add_filter('tag2', 'word_swap');

so, the example 2 is a bad usage example if your allowed filter array doesn't contain almost all the filters.

But if you REALLY want your filter to be called for almost all the cases (maybe excluding some of them), or if you want to do something on development phase like tracking the filter execution, then it is OK.

The "all" filter is always executed before the filter itself, so if there's such code

add_filter('all', 'word_swap');
add_filter('some_filter', 'other_function');

your function word_swap will always be called before the other_function. You can even use add_filter inside your function word_swap like this.

function get_tags_to_exclude(){
    return ['some_filter_to_exclude_1', 'some_filter_to_exclude_2'];
}
function word_swap_wrapper($tag, $value = null){
    add_filter($tag, 'word_swap');
    return $value;
}
add_filter('all', 'word_swap_wrapper');
function word_swap($value = null){
    if( in_array( $tag, get_tags_to_exclude() ) ){
        return $value;
    }
    // do whatever you want here
    return changed_value;
}

I guess you don't want it if you're going to filter everything totally. In the case you want your function word_swap to work after all the other callbacks are executed, then you can use priority like this:

function get_tags_to_exclude(){
    return ['some_filter_to_exclude_1', 'some_filter_to_exclude_2'];
}
function word_swap_wrapper($tag, $value = null){
    add_filter($tag, 'word_swap', PHP_INT_MAX);
    return $value;
}
add_filter('all', 'word_swap_wrapper');
function word_swap($value = null){
    if( in_array( $tag, get_tags_to_exclude() ) ){
        return $value;
    }
    // do whatever you want here
    return changed_value;
}

I don't know a better way to register a callback to be executed the last, so please fix me anybody.

Let's take a look at the function word_swap_wrapper, it will register word_swap function for each time the for each tag, to restrict that we can check it before registering the word_swap for the same tag again and again. So the final solution will be:

function get_tags_to_exclude(){
    return ['some_filter_to_exclude_1', 'some_filter_to_exclude_2'];
}
function word_swap_wrapper($tag, $value = null){
    static $lookup;
    if(!$lookup){
        $lookup = [];
    } elseif( !isset($lookup[ $tag ]) ){
        $lookup[ $tag ] = true;
        add_filter($tag, 'word_swap', PHP_INT_MAX);
    }
    return $value;
}
add_filter('all', 'word_swap_wrapper');
function word_swap($value = null){
    if( in_array( $tag, get_tags_to_exclude() ) ){
        return $value;
    }
    // do whatever you want here
    return changed_value;
}

You can find a not complete list of WP predefined filters here and here

  • Please explain what the code does and how it answer the quesiton. Just dumping code is not considered a good answer. – cybmeta Jul 11 '17 at 7:56
  • Good to know for trivia, but please no one actually do this in a real site. – Rarst Jul 11 '17 at 7:57
  • Do you know the hooks reference in the developer portal? It is updated for latest WordPress version, not like the filters list you linked to. – cybmeta Jul 11 '17 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.