0

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:

www.example.com/?argument_1=test&argument_2=test2

Or any page like this:

www.example.com/anotherpage/?argument_1=test&argument_2=test2

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.

2
  • 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[] Commented 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. Commented Jan 21, 2013 at 9:29

1 Answer 1

0

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;
        $wp_rewrite->flush_rules();
    }
}

// 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.

4
  • 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. Commented 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. Commented Jan 21, 2013 at 9:49
  • Does this require .htaccess to be modified? It is not clear in the docs. Commented 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 Commented Jan 21, 2013 at 9:54

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.