0

I have the following code in my functions.php file, and it simply will not work to return the value passed via the URL. This is the code below:

    function custom_url_source() {

        add_rewrite_rule(
            '^source/([^/]*)$',
            'index.php?source=$matches[1]',
            'top'
        );

        add_rewrite_tag('%source%','([^/]*)');
    }

    add_action('init', 'custom_url_source');


    var_dump($wp_query->query_vars);

So by typing 'http://example.com/source/the-times/ into the URL I'm expecting to see 'source' => 'the-times' in the var dump, but I see an empty array.

I have flushed my rewrite rules, and the standard .htaccess is in place. The server is a standard LAMP configuration.

Any indicators would be great. I've tried various solutions outlined on here and had no luck. I've Googled and the examples given don't work.

1

Try adding 'source' as an custom query var, rather than defining it as a rewrite tag.

function wpse162627_add_query_vars( $query_vars ){
    $query_vars[] = "source";
    return $query_vars;
}
add_filter( 'query_vars', 'wpse162627_add_query_vars' );    

function custom_url_source() {

     add_rewrite_rule(
         '^source/([^/]*)$',
         'index.php?source=$matches[1]',
         'top'
     );

}
add_action('init', 'custom_url_source');

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.