Get post name from permalink
-
I’m writing a plugin that passes information to a post or page via URI segments. I have added a URI segment that I use to differentiate between the permalink for the post/page and the parameters I am passing. For example:
https://foo.bar/permalink/X/param1/param2
I have added rewrite rules for pages with no issue, however posts are giving me a problem. The reason is I don’t know how to extract the post name from the permalink definition to generate a proper regex. Something like:
‘(.+?)/X/(.*)’ => ‘index.php?name=’$wp_rewrite->preg_index(1).’¶ms=’.$wp_rewrite->preg_index(2);
Works great for the permalink structure of /%postname% but will obviously break if I use ‘/%category%/%postname%’ since the name query_var will be passed the category as well and will 404. So, based on a permalink definition does WordPress have a way of giving me a regex that will result in the post name? Or is there a better way of doing this, such as somehow stripping off the extra parameters higher up in the action chain?
- The topic ‘Get post name from permalink’ is closed to new replies.