[wp-hackers] Taking over URL space

Chris Williams chris at clwill.com
Wed Feb 29 17:14:00 UTC 2012


You need to go into the permalinks setting page, save the changes (even
without changing anything) and then your rewrite rules get invoked and
written.

On 2/29/12 5:07 AM, "Phillip Lord" <phillip.lord at newcastle.ac.uk> wrote:

>
>
>Thanks all for the comments. I have been trying these out, and with info
>from the WPrewrite documentation but can't get them to work.
>
>
>My plugin (complete with debug printouts!) looks like below. I have the
>template_redirect section working fine. I have a rule flusher on
>wp_loaded. The codex mentions this not as a long term solution because
>it's compute intensive.
>
>Then I have a generate_rewrite_rules filter. As far as I can tell it's
>never been called.
>
>I would like to try the add_rewrite_rule/add_rewrite_tag method, but I
>don't understand this. What are these two methods doing?
>
>Phil
>
>
>
>add_action( 'wp_loaded','my_flush_rules' );
>
>// flush_rules() if our rules are not yet included
>function my_flush_rules(){
>    print( time() + "\n" );
>    print( "4: my flush rules\n" );
>	$rules = get_option( 'rewrite_rules' );
>
>	if ( ! isset( $rules['test'] ) ) {
>        print( "2: isset\n" );
>		global $wp_rewrite;
>	   	$wp_rewrite->flush_rules();
>	}
>}
>
>
>add_filter( 'generate_rewrite_rules','test_phil_insert_rewrite_rules' );
>
>// Adding a new rule --- never called
>function test_phil_insert_rewrite_rules( $wp_review )
>{
>    print( "insert rewrite rules\n" );
>	$newrules = array
>        (
>         "test" => 'index.php?test-plugin=1&hello-world=1',
>         );
>	
>	return $newrules + $rules;
>}
>
>
>
>// this bit is working
>add_filter( 'query_vars', 'test_query_vars' );
>
>function test_query_vars( $query_vars ){
>    print( "test_query_vars\n" );
>    $query_vars[] = "test-plugin";
>    $query_vars[] = "hello-world";
>
>    return $query_vars;
>}
>
>
>// this bit handles the request and it is working
>// as http://localhost/?test-plugin=1&hello-world=1 returns hello world
>function test_plugin_hello(){
>    print( "test_plugin_hello" );
>    $test_plugin = (boolean) get_query_var( 'test-plugin' );
>    $hello_world = (boolean) get_query_var( 'hello-world' );
>    if( !$test_plugin ) return;
>
>    print( "Hello World" );
>    exit();
>    
>}
>add_action( 'template_redirect', 'test_plugin_hello' );
>
>
>
>Braydon <ronin at braydon.com> writes:
>
>> Here is another way to have new rules without using add_rewrite_rule:
>>
>> add_action( 'generate_rewrite_rules', 'my_rewrite_rules' );
>>
>> function my_rewrite_rules(){
>>     global $wp_rewrite;
>>     $rules = array(
>>         'myplugin/?$' => 'index.php?myplugin=1'
>>     )
>>     $wp_rewrite->rules =array_merge($rules,$wp_rewrite->rules);
>> }
>>
>> For using extensions on p, something like this could work:
>>
>> add_action('parse_request','my_parse_request' );
>>
>> function my_parse_request( &$request ) {
>>     $post_id = $request->query_vars['p'];
>>     // check if the post id has an extension
>>     // also check the "Accept:" header
>>     // add a new query_var for the extension, and change "p" variable
>>back to
>> an id
>> }
>>
>> However adding a new query_var for extension would be needed either way.
>>
>> add_filter( 'query_vars, 'my_query_vars' );
>>
>> function my_query_vars( $query_vars ){
>>     $query_vars[] = 'myplugin';
>>     $query_vars[] = 'ext';
>>     return $query_vars;
>> }
>>
>> And then at the template redirect check for both the myplugin and ext
>> query_vars and then have your plugin handle those.
>>
>> On 02/21/2012 01:58 AM, Phillip Lord wrote:
>>> I was wondering whether someone could give me pointers in the right
>>> direction. I'm not after a complete solution! I was just hoping that
>>> someone with more knowledge of wordpress could tell me if I am heading
>>> in the right way.
>>>
>>> I wanted to write a new plugin which returns metadata about posts in a
>>> variety of different ways. To do this, I need to use various parts of
>>> the URL space, but I don't know how best to do this.
>>>
>>>
>>> Essentially, I want to do three things.
>>>
>>> 1) I would like to be able to use bits of the URL space from top level.
>>>     So, for a wordpress at
>>>
>>> http://mydomain.org.uk
>>>
>>> I would like to be able to serve all requests to..
>>>
>>> http://mydomain.org.uk/myplugin
>>>
>>> My initial idea was to achieve all this with rewrite rules, but
>>> requiring users to modify .htaccess com. wp-rewrite seems the next
>>> option -- I can just rewrite requests to this and underlying rewrite
>>> plugin php?
>>>
>>>
>>> 2) I want to add support for file extension based access. So for a
>>> URL such as
>>>
>>> http://mydomain.org.uk/?p=46
>>>
>>> I'd like requests to
>>>
>>> http://mydomain.org.uk/?p=46.bib
>>>
>>> to be handled by my plugin, probably by sending the browser straight to
>>> a URL of the http://mydomain.org.uk/myplugin. I am not sure how well
>>> this would interact with pretty permalinks -- something of the form
>>>
>>> http://mydomain.org.uk/2012/02/02/mypost/
>>>
>>> looks daft with an extension
>>> http://mydomain.org.uk/2012/02/02/mypost/.bib
>>>
>>> However, as far as I can see this is a similar problem to the last.
>>>
>>> 3)
>>>
>>> And, finally, I'd like to provide a content negotiated solution. So
>>> requests to:
>>>
>>> http://mydomain.org.uk/?p=46
>>>
>>> will return different results depending on the content type in the
>>> Accept: header of the request; again, this will probably be implemented
>>> by forwarding the browser.
>>>
>>> This one seems to be the most taxing -- I can't do this with rewrite
>>> rules because it is the same in all cases.
>>>
>>> Any pointers gratefully received!
>>>
>>> Phil
>>>
>>> _______________________________________________
>>> wp-hackers mailing list
>>> wp-hackers at lists.automattic.com
>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>
>>> !DSPAM:4f436acf300322900114539!
>>>
>
>-- 
>Phillip Lord,                           Phone: +44 (0) 191 222 7827
>Lecturer in Bioinformatics,             Email:
>phillip.lord at newcastle.ac.uk
>School of Computing Science,
>http://homepages.cs.ncl.ac.uk/phillip.lord
>Room 914 Claremont Tower,               skype: russet_apples
>Newcastle University,                   msn: msn at russet.org.uk
>NE1 7RU                                 twitter: phillord
>_______________________________________________
>wp-hackers mailing list
>wp-hackers at lists.automattic.com
>http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list