0

Hey all I'm a WP noob and I have the task of cleaning up the mess other people left when they edited plugin cores. To my understanding, there are such things called filters in WP. I am not sure how to use them though or how they work. For this example below (copied from a tutorial):

function remove_footer_admin () {
echo 'Fueled by <a href="http://www.wordpress.org" target="_blank">WordPress</a> | Designed by <a href="http://www.uzzz.net" target="_blank">Uzzz Productions</a> | WordPress Tutorials: <a href="http://www.wpbeginner.com" target="_blank">WPBeginner</a></p>';
}

add_filter('admin_footer_text', 'remove_footer_admin');

What is going on? So we are overwriting the admin_footer_text function? In this case, it's a function from the admin section. To my current understanding (tell me if I'm wrong please), the admin section has loads of default WP functions that are executed in a certain order. This add_filter that uses remove_footer_admin will be called as an after hook. So whatever WP functions run before admin_footer_text will run accordingly, then admin_footer_text, then remove_footer_admin, then whatever WP functions that usually run after admin_footer_text. Is this correct?

Also, it seems like add_filter can only be added sometimes? I don't get what the conditions are. Could you please explain in what situation I can use it?

  • 1
    How is this different from your other question and what do the answers to that question not explain? – s_ha_dum Jun 25 '13 at 14:20
  • I still don't get when the filter is run in the context of the function that I am add_filtering to. Is it sort of like adding a few lines of code to that function and running them before the end of the function? – Edmund Jun 25 '13 at 14:33
  • That's actually a pretty bad example of what a filter should do. The rule of thumb that I follow is that a filter should return something, not echo it. If you want to echo something, then use add_action() instead. – Pat J Jun 25 '13 at 16:27
1

I like to think of add_filter as a way for you to adjust a variable. It can be used anywhere you see an apply_filters in the code. Typically, you return a variable value instead of echoing it out.

Your example would be better written like so:

function remove_footer_admin () {
return 'Fueled by <a href="http://www.wordpress.org" target="_blank">WordPress</a> | Designed by <a href="http://www.uzzz.net" target="_blank">Uzzz Productions</a> | WordPress Tutorials: <a href="http://www.wpbeginner.com" target="_blank">WPBeginner</a></p>';
}

add_filter('admin_footer_text', 'remove_footer_admin');

And remove_footer_admin() would be run when the WP code hits the

echo apply_filters( 'admin_footer_text', '<span id="footer-thankyou">' . __( 'Thank you for creating with <a href="http://wordpress.org/">WordPress</a>.' ) . '</span>' );

in admin-footer.php...where the second parameter (<span> and so forth) is the variable that apply_filters is allowing you to modify in this instance.

I wrote a tutorial on WordPress filters that I think might help explain some things.

  • Great!!! thanks a lot! I'll check out your tutorial!! – Edmund Jun 25 '13 at 14:55

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.