How to best handle permalink rules for dynamic plugin pages
-
I’ve currently run into a dillema where a plugin i’ve written detects a specific URL request on init hook, checks the url parameters and runs a query to overwrite the loaded post based on that, as well as parses the url for parameters to send into the plugin.
Note: this whole situation is only applicable when permalinks are turned on. The plugin gracefully handles not having permalinks on, by generating get variables instead of the below examples. The point is to take advantage of WordPress’s handling of permalinks to make this work.
Basically how it works is as follows:
– Plugin loads, runs hook on init
– Detects whether url contains ‘/query/’ pattern in it eg. http://www.example.com/wordpress-page-name/query/parameter/parameter/
– If pattern detected, then grab url prefixed to that eg. http://www.example.com/wordpress-page-name/ and run get_page_by_path() with that url to grab the page object, then run a query_posts with that page_id which alters the main query currently running.
– Plugin code then has access to these variables and passes it to the shortcode content that runs on the post content that belongs to that page, displaying dynamically relevant content.This does work. However there are problems with this approach.
The problem is when a plugin or code or widget runs another query_posts on the page, and runs wp_reset_query() that query is lost, despite the fact that I want it to be the main query for the page.
Because originally when wordpress starts and tries to determine the post for the page that url obviously cannot match any wordpress articles due to the added parameters, the main query basically returns no results/error 404, which gets overridden with the query_posts therefore page content shows.
The only solutions I’ve found for this so far are kludgy at best which involve:
– Running a second hook on pre_get_posts, then checking it is_main_query(), check if url matches pattern above, then over-writing the main query object with the WP_Query object created by the query_posts() function (after having stored it in a global variable).
This works well, however I think it’s kludgy, dangerous, may not work in the future, not really future-proof and possibly not even future-release compatible.
– Write custom .htaccess rules to handle it – I don’t like this method as not every client who uses this plugin may necessarily have a writeable .htaccess file, or even use apache (they could use IIS – as one of our clients does).
– Edit every theme that uses wp_reset_query(), and replace it with code to reset it with the query that displays the content we want – which is unmaintainable really and not really compatible with most themes and other plugins that may be installed.
– Use WP_Rewrite – I haven’t been able to find anything to suggest that this approach may work, however everything I’ve read so far doesn’t seem to indicate that this may necessarily work either. I don’t have a whole lot of experience with WP_Rewrite class so I’d appreciate someone else’s knowledge/experience giving me some input into this.
I’m looking for a solution that preferrably is:
– Not dodgy/kludgy, involve re-writing of global variables in my own code, rather than use WordPress API’s to handle things correctly.
– Works with wordpress permalinks
– Works inside of wordpress in a plugin – not some dodgy other page with wordpress headers/footers etc.
– Doesn’t involve modifying or rewriting themes
– Doesn’t involve manipulating .htaccess or other webserver configurations beyond the default wordpress setup
- The topic ‘How to best handle permalink rules for dynamic plugin pages’ is closed to new replies.