Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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[] –  BandonRandon Jan 21 '13 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. –  Codex Meridian Jan 21 '13 at 9:29
add comment

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
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. –  Codex Meridian Jan 21 '13 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. –  BandonRandon Jan 21 '13 at 9:49
    
Does this require .htaccess to be modified? It is not clear in the docs. –  Codex Meridian Jan 21 '13 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 –  BandonRandon Jan 21 '13 at 9:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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