Most plugins mention adding add_filter to functions.php, but can I use it elsewhere? Does it depend on the plugin?

I am trying to use add_filter in a custom template file before get_header(); but it doesn’t look like its being added on time.

I guess I am trying to figure out the order of when things run, so that I can call it at a point where it’s available.

1 Answer
1

add_filter() and add_action() are available before any plugin is loaded. So you can use both in the first line of your plugin or theme.

For readability I recommend to group action and filter registrations at the very top of your main file:

in a plugin, the file with the plugin header
in a theme the functions.php
There are exceptions for that rule:

Chained callbacks. In this example I register an action for shutdown only when the first filter for wp_nav_menu_objects has been called. So the second callback cannot be registered at the same time as the first one.

to conclude it is possible to a certain point but it’s not recommanded

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *