customization – display text generated via custom get query string

Question

You don’t need to use the query var API for that, just use $_GET or filter_input like any other standard PHP code. The query variable API is for rewrite rules and WP_Query, e.g. for handling custom permalinks and virtual pages, etc. It was never intended for retrieving raw URL parameters in templates like that.


As for why your example does not work, the problem is that by the time your page template has loaded, the query variables filter has already run! The main query has already ran, it’s already fetched your page, and already decided on the template to load. Your filter has been added yes, but the filter ran a long time ago, your code is waiting to board a train that has already left!

Generally, actions and filters don’t belong in templates and are almost always a sign that a mistake has been made or that the user doesn’t understand that actions/hooks/filters aren’t a domain language, and that events on a timeline is a more accurate mental model. add_actionn( 'init', 'foobar' )' is equivalent to _from now on, if the init event happens, call foobar.

It’s not what you did, it’s when you did it. Put your filters/actions in a plugin or functions.php instead

0
funkoolow 3 weeks 2021-05-25T17:44:38-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse