Take the 2-minute tour ×

I am a bit confused about how WordPress's permalink works, especially beyond Wordpress's own usage. My permalinks are like:


But in single.php I want to put another link to page itself but with different query string. When it is clicked the permalink structure may look like:


I want to get the value from $_GET['action'], so:

$_GET['action'] = %mystring%

my plan is to interpret it like:

if('xx' == $_GET['action']){
   //do xx stuff
} else if ('yy'==$_GET['action']){
   //do yy stuff
} else {
   //show the single post as a single.php always shows

that means, I want to parse the $_GET['action'] optionally. If I do not parse it even if it is available in query string, I want the page to be rendered correctly.

So to get this done, where should I actually work? Also how do I form the link for <a> tag? Usually we make link this way:

<a href="'.the_permalink().'">TEXT</a>

but you already know, I need to add some text before the original permalink of post.

Thanks in advance.

share|improve this question

1 Answer 1

Leave your permalink structure as it was and check out my answer on custom rewrite rules.

You could adapt the code like so;

function my_rewrite_rules($rules)
    global $wp_rewrite;

    // the key is a regular expression
    // the value maps matches into a query string
    $my_rule = array(
        '(.+)/(.+)/?$' => 'index.php?pagename=matches[2]&my_action=$matches[1]'

    return array_merge($my_rule, $rules);
add_filter('page_rewrite_rules', 'my_rewrite_rules');

function my_query_vars($vars)
    // this value should match the rewrite rule query paramter above

    // I recommend using something more unique than 'action', as you
    // could collide with other plugins or WordPress core

    $my_vars = array('my_action');
    return array_merge($my_vars, $vars);
add_filter('query_vars', 'my_query_vars');

Now the page my_page should be available at http://example.com/whatever/my_page and http://example.com/my_page.

You can get the value of whatever using get_query_var('my_action').


This may have undesired effects when viewing children pages or page attachments. You could get around this by passing an identifier in your rewrite, something to the effect of;


Note: You will need to edit the rewrite rule if you wish to do this. Every time you make changes to the code you will need to re-save your permalink structure to 'flush' the rules.

share|improve this answer
Finally got this working. Thanks dude! –  iKindred Sep 2 '10 at 6:24
+1: For disclaimer :) –  shamittomar Sep 17 '10 at 18:13
great, this is what I was looking for. I'll test it tonight! –  woony Oct 2 '12 at 8:35
In case it helps anyone, I had to escape the fwd slashes in my redirect regex rules before they would match. For example ^info/([^/]+)/?$ becomes ^info\/([^\/]+)\/?$ –  mike Oct 5 at 20:36

Your Answer


