This is my case. Supposing in my plugin dB table I have the following stored field name and values:

argument_1= 'test'
argument_2= 'test2'

And I want to add a new string query string variables to all links going out to www.example.com such that it will look like this:

If homepage is accessed:


Or any page like this:


What Wordpress hooks, actions and filters would I be using to retrieve the arguments and values in the database and append it to a hyperlink URL if it matched the domain, e.g. example.com ?

I am thinking of using add query arg but I'm confused a bit on how this function relates to this application. Any tips and advices are highly appreciated..Thanks.

  • What are the arguments? Meta_data, dates, tags? This is important on how to call them in the_loop but you may want to check out codex.wordpress.org/Class_Reference/WP_Query and php's $_GET[]
    – Brooke.
    Jan 21, 2013 at 9:28
  • No, they are not meta_data, dates or tags. They are pure values stored in a custom wordpress database table, not in post meta. Jan 21, 2013 at 9:29

1 Answer 1


Sorry I read your question wrong. You probably want to use WP_Rewrite and do something like:

add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );

// flush_rules() if our rules are not yet included
function my_flush_rules(){
    $rules = get_option( 'rewrite_rules' );

    if ( ! isset( $rules['(argument_1=test&argument_2=test2)$'] ) ) {
        global $wp_rewrite;

// Adding a new rule
function my_insert_rewrite_rules( $rules )
    $newrules = array();
    $newrules['(argument_1=test&argument_2=test2)$'] = 'index.php?argument_1=$matches[1]&argument_2=$matches[2]';
    return $newrules + $rules;

// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars )
    array_push($vars, 'id');
    return $vars;

this is untested and mostly stright from the docs but should get you on the right track.

  • Thanks Bandon :).So if I view the URL in the sourcecode, it will now be rewritten like this? www.example.com/anotherpage/?argument_1=test&argument_2=test2 or it will still display the original URL but it will only be redirected to this new URL? I am not familiar with wp_rewrite. Jan 21, 2013 at 9:46
  • That particular rule will require something like anotherpage/?argument=1&argument=2(although i forgot the & in the code) you could also do anotherpage/1/2 then rewrite it to have your args if you desired.
    – Brooke.
    Jan 21, 2013 at 9:49
  • Does this require .htaccess to be modified? It is not clear in the docs. Jan 21, 2013 at 9:50
  • if you use the filters above, no. If you don't want to use filters then yes (see example 2 in docs) It's either filters or .htaccess but really no need for both. If you want to talk more I created this chatroom: chat.stackexchange.com/rooms/7180/wpse-82459
    – Brooke.
    Jan 21, 2013 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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