It’s not like I invented a new way to slice bread, the idea of filtering a define statement’s value where the constant could be used as an option just seemed to make a good deal of sense. I also decided to use the idea in the next release of Opus Primus.
Here’s the premise: there are several instances where a default condition, to be used as a display option, are checked if they are true or false. This was being handled awkwardly. It was being done by calling a class object global; and, checking the class method which was simply a single line return true type of function. I wanted this to be a bit easier, especially for Child-Themes to handle, without having to overload the class method and/or re-write the class altogether. This lead to the following idea (only the names have changed from actual use):
Let’s look at this as an example:
define( 'DISPLAY_AUTHOR_DESCRIPTION', true );
This is all good; except, how do you over-write the define statement with a minimal amount of code?
Filter it! Give this a try instead of the above:
define( 'DISPLAY_AUTHOR_DESCRIPTION', apply_filters( 'display_author_description_bool', true ) );
This does exactly the same thing as the code above but it now allows you to use the add_filter function in a Child-Theme to easily change this to false … and, of course, you can choose whatever filter handle you want to use, I just appended _bool to the constant name to ensure they are different but also related and to note it should be a boolean value.
For a Child-Theme, you just need to add this in its functions.php template file:
add_filter( 'display_author_description_bool', '__return_false' ) );
This uses the __return_false function I noted in this article rather than writing a callback function that does the same thing. Note, add_filter requires a callback function for its second parameter, passing false on its own may return an error message.