Custom rewrite approach with add_rewrite_endpoint()
-
Hello everyone…
I’ve been trying to figure out the wordpress rewrite engine during the last two days. My aim is / was to create a “trackback” like uri endpoint which doesnt need any parameters to function and thereby manage template redirects for a custom print view page. I wanted to keep it as simple as possible and thus tried to use the function
add_rewrite_endpoint()
.This is what I got so far:
function printview_activation() { global $wp_rewrite; add_rewrite_endpoint('printview', EP_ALL); $wp_rewrite->flush_rules(); } register_activation_hook( __FILE__, 'printview_activation'); function printview_deactivation() { global $wp_rewrite; $wp_rewrite->flush_rules(); } register_deactivation_hook( __FILE__, 'printview_deactivation'); function printview_query_vars($vars) { array_push($vars, 'printview'); return $vars; } add_filter('query_vars','printview_query_vars');
The cool thing is: It works and it REALLY is a great alternative to creating complex custom rewrite rules (it was quite hard to figure out the whole functionality of the rewrite engine since there are no real tutorials on the web). One problem remains however and since I am kind of a perfectionist I would really like to make that last step too:
In order to make it function the URL has to look like https://mywordpress.com/random-page/printview/1 (or any other parameter) since wordpress doesnt include any endpoints that have no value in the query string. ($wp_query->query_vars[‘printview’]; returns null if it doesnt have a value).
And here comes my question: Is there an easy way to make it work so I dont have to use any parameters on the printview endpoint without using the generate_rewrite_rules function?
This is a post I found on a similiar topic (it explains why the query_var only gets set when it has a value): https://old.nabble.com/Rewrite-endpoints-td25232037.html
Maybe someone on this forum nows a nice approach on how to make wordpress recognize the query_var even if its “empty” (like it does for “/trackback/”) without having to change the core files.
It should work the same way “trackback” works: “If ‘/trackback/’ exists make it ‘trackback = 1’ (‘tb = 1’ in the code)”.
Thanks!
- The topic ‘Custom rewrite approach with add_rewrite_endpoint()’ is closed to new replies.