[wp-hackers] Taking over URL space

Daniel Grundel daniel at webpresencepartners.com
Tue Feb 21 15:03:00 UTC 2012


Not sure about 3, but I would handle 1 and 2 with the same mechanism. For
#2, I'd just check to see if there's a file extension at the end of the
URL. Something like is working for me now:

You need to add_rewrite_tag for each variable/piece of data you want to
read from the query string. Then you can use those when adding your rewrite
rule. To actually *do something* with that, you can hook into
template_redirect and check to see if your query var(s) are set and if so,
do something. If you want to output something like a CSV or other file, you
can just set your header (including content-disposition!) properly, dump
your text out, and exit().

Also, after you activate your plugin, you will need to manually flush your
rewrite rules by going into your permalink settings and clicking the Save
button for the rewrites to start working.

<?php

    function myplugin_add_rewrites() {
        add_rewrite_tag('%my_plugin_init%','([^&]+)');
        add_rewrite_tag('%my_variable%','([^&]+)');

        add_rewrite_rule('myplugin/([^/]*)/?
','index.php?my_plugin_init=1&my_variable=$matches[1]','top'); // so,
myblog.com/myplugin/some_data will give you my_plugin_init =1 and
my_variable = "some_data" when you do_something() below.
    }
    add_action('init', 'myplugin_add_rewrites');


    function myplugin_do_something() {
        $my_plugin_init = (boolean) get_query_var('my_plugin_init');
        $my_variable = get_query_var('my_variable');

        if(!$my_plugin_init) return; // 'myplugin' wasn't found in our URL

        //do something here...
        //you can dump data out to the browser or set a header() to
redirect, just make sure you exit() when you're done if you do, unless you
also want WP to render whatever post/page should be rendered.
    }
    add_action('template_redirect', 'myplugin_do_something');
?>

Probably a little vague. I have very, very similar code in place and
working right now that took a ton of trial and error to work out. Tinker
with this a little bit and if you have more questions, let me know.

Hope that helps!

Daniel J. Grundel
Web Presence Partners
webpresencepartners.com
daniel at webpresencepartners.com
772 678 0697



On Tue, Feb 21, 2012 at 4:58 AM, Phillip Lord
<phillip.lord at newcastle.ac.uk>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
>


More information about the wp-hackers mailing list