1

Im building a minor website and i want whenever a person uses a certain link, he gets redirected to the frontpage - and a formular opens.

fx: www.homepage.com/example/ -----> www.homepage.com

In the past i have used add_rewrite_rule to redirect people with custom variable - and i can easily redirect them with just using:

add_rewrite_rule('^example/?','index.php','top');

But my "problem" is that i need a static variable, so i figured i could just do as follows:

 add_rewrite_rule('^example/?','index.php?key1=123','top');

Sadly i seem unable to get this to work, and when trying to just query_var('key1') - i get nothing. What am i doing wrong here?

Update 1 Trying to whitelist a query variable, as mentioned in comments

add_action('init', 'test_rewrite_rule', 10, 0);
function test_rewrite_rule() 
{
    add_rewrite_rule('^example/?','index.php?p=1&my-api=test','top');
}

//add_action('init', 'test_rewrite_tag', 10, 0);
//function test_rewrite_tag() 
//{
    //add_rewrite_tag('%my-api%', '([^&]+)');
//}

add_filter( 'query_vars', 'wpd_query_vars' );
function wpd_query_vars( $query_vars )
{
    $query_vars[] = 'my-api';
    return $query_vars;
}

and to get the variable

  $wp_query->query_vars['my-api'] 

Update 2

To make it perfectly clear, i dont really care what variable it passwed with my rewrite route, as long as im able to see that it got redirected :)

  • 2
    Have you whitelisted that key? – Tom J Nowell Sep 4 '17 at 10:44
  • 1
    whitelisted the key? – Mac Sep 4 '17 at 11:56
1

You can't add arbitrary query variables to the rewrite rules, there's a whitelist of allowed variables.

To add a query variable, use query_vars filter, e.g. this filter adds my-api as a valid query variable:

function wpd_query_vars( $query_vars ){
    $query_vars[] = 'my-api';
    return $query_vars;
}
add_filter( 'query_vars', 'wpd_query_vars' );

But that just gets you the query variable, you still need to do something with it. Rewrite rules map pretty URLs on to query variables, nothing more. That's why a rewrite rule that doens't map to index.php won't work.

Instead, you then need to write a hook that looks for when your new query variable is defined, and if so, do what you want that page to do.

Most people will use the template_redirect hook and load a custom template, e.g.

add_action( 'template_redirect', 'custom_page_template_redirect' );
function custom_page_template_redirect() {
    global $wp_query;
    $custom_page = $wp_query->query_vars['my-api'];
    if ( $custom_page == 'key' ) {
        // we've found our page, call render_page and exit
        echo "hooray! it works!";
        exit;
    }
}

Where instead of outputting Hooray, or loading a template, you'd call wp_safe_redirect() to redirect the user to a destination of your choice ( or wp_redirect if it's offsite )

  • I also wrote up query vars on wp the right way at wptherightway.org/en/routing/… – Tom J Nowell Sep 4 '17 at 15:05
  • I tried that, and it doesnt seem to work sadly - i still get a "unindentified index". I have updated OP with the code where i whitelist my query vars. – Mac Sep 5 '17 at 6:35
  • unidentified index is not a WordPress error, can we see the code you're using to test this? – Tom J Nowell Sep 5 '17 at 10:23
  • Look at the updated paragrahp in OP, thats the exact code i use – Mac Sep 5 '17 at 11:09
  • @Mac there's code missing from your question, we never see the part where you use the query var, only the rewrite rule and the whitelist, it's important we see the code that uses the query var, not just the registering and the rewrite rule, right now it's not possible to debug, for all I know you're doing $_GET['my-api'] instead of fetching the value, but without seeing I can't tell – Tom J Nowell Sep 5 '17 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.