I am very new to Wordpress Development so this question might be silly .May be I do not understand the concept of custom filters in Wordpress . So far I have used Wordpress's own filter hooks to change values

add_filter('the_content', 'ffl_add_footer') ; // add footer to the blog content

I have just started learning about custom filters that you can code . So my understanding is you use apply_filters() to set up a filter hook to a value like below

apply_filters('filter_tag' , $var ) ;

later you hook into that filter and use a callback function to modify the value ($var) like below

add_filter('filter_tag , 'callback') 

Your callback would be like below :

function callback($var) {
//modify $var 
return $var


So I was testing this understanding with a code like below :

function callback($var){
    return ($var.'append');

$var = 'testing';
echo $var;
$var1 = apply_filters('custom_filter', $var);
add_filter('custom_filter' , 'callback');
echo $var1;

This echoes testingtesting while I was expecting testingtestingappend . Am I understanding this wrong or is there something wrong with my code ?

1 Answer 1


I believe your the simple nature of your example may have led to a misunderstanding of the way appy_filters() would operate. There is a great article that really explains how to use add_filter() and apply_filters() well, using very readable examples.

Basically, the problem appears to be that your code declares add_filter() AFTER you define it with apply_filters(). This is an "order of operations" error.

Perhaps this annotated version can help:

// declare your filter function and callback .. usually pretty close to each other.
add_filter('ex1_append_text', 'callback_appender');
function callback_appender($initial_text){
    return $initial_text.' ... whatever ...';

// setup basic variable, as you did
$var = 'testing';
echo $var;

// apply the filters we declared above
$modified = apply_filters('ex1_append_text',$var);
echo $modified;
  • BTW ... you are encouraged to add some unique letter combination, or abbreviation, to the beginning of filter and action names, to decrease the likelihood of collisions with other functions/plugins, etc. So, in my example, I used 'ex1_'. You might have used 'kno_' (taken from your screen name). Aug 28, 2019 at 18:41
  • Thanks for the answer . I thought it worked the other way . However , now I have a related question . What happens when you try to hook into a filter of an existing plugin (say plugin named B) . How do I ensure my plugin's add_filter() is executed first and later B's apply_filter() is executed . The plugins seems to run in an alphabetical order . Does that mean my plugin needs to be named A ?
    – Knownow
    Aug 29, 2019 at 8:54
  • Properly written plugins use wordpress hooks to set the order, and should not care about the execution order. A plugin that modifies content or display elements is building a queue which will recognize priorities set by action hooks. If you would like me to provide sample code, start a new question regarding Plugins and Action Hooks. I'll be happy to provide examples. If you still want to order plugin operations Aug 29, 2019 at 16:51
  • P.S. When someone provides a response that directly answers your initial question, it is polite to mark it as the answer ... (hint .. hint) Others will return the favor, if you provide answers. Aug 29, 2019 at 16:53
  • Sorry I missed marking your answer . I have started a question in SO , may be I am not understanding this right . Could you take a look into it ?stackoverflow.com/questions/57708148/…
    – Knownow
    Aug 30, 2019 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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