wordpress url resolution of subpages from within p

2019-05-26 15:16发布

Say I have a Page "foo" in wordpress such that http:/www.blah.com/foo brings it up.

Say this page has a trigger within its content such as <!-- foo --!> which is being tracked by my plugin through the add_filter('the_content', ..) filter. So, all calls to http:www.blah.com/foo and http://www.blah.com/foo?var=bar are picked up by the plugin and dealt with.

Now is there a way by which the plugin could let wordpress know that it wants to handle all urls of the type http://www.blah.com/foo/bar http://www.blah.com/foo/bar/morefoo etc.. "without" having to create pages/subpages for each of these as these urls would be created dynamically?

Also, is there anyway besides using the add_content filter on the_content within a page that one can grab control from within a plugin ideally keyed of the url so all calls to http://www.blah.com/foo are handled by the plugin.

3条回答
甜甜的少女心
2楼-- · 2019-05-26 15:31

I do something very similar to what you describe using a custom mod_rewrite rule. Following your example, I have a page Foo that uses the template foo.php. The foo.php template accepts a query variable, say "path", that defines the remainder of the URL. In my .htaccess file, I have the following rule (be careful to put it outside WordPress' automatically-generated rules!):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^foo/(.+) /foo/?path=$1 [L] 
</IfModule>

Your plugin may also need to disable the "redirect_canonical" plugin, which can poach your URLs unexpectedly.

remove_action('template_redirect', 'redirect_canonical');

I think it may also be possible to do something similar using the template_redirect hook and WP_Rewrite class, but I have not tried it myself.

查看更多
爷、活的狠高调
3楼-- · 2019-05-26 15:32

Yes, you can grab URL references (aka queries) and parse them, without creating subpages.

The sequence of events is described in the Query Overview page. You want to look at hooking into the parse_request action in particular.

查看更多
劳资没心,怎么记你
4楼-- · 2019-05-26 15:42

Answering my own q:

The pointers in the answers above were useful and got me going on the right track, but I kept hitting a snag in that whenever the url in question was invoked it kept calling index.php.

I then came across http://mikeschinkel.com/blog/restful-web-services-in-wordpress-plugin/ where he goes into an explanation and provides an answer that requires a template_redirect action to ensure that control is where you want it.

查看更多
登录 后发表回答