1

As part of a project I'm required to make a wordpress plugin which integrates with Google for authentication (There are good reasons for developing a new plugin instead of using an existing one). If the user decides to cancel the login during the authorisation process then google will redirect back to my site and will set a GET variable named 'error'.

So, I noticed that $_GET['error'] was never available and, after a little bit of research, I came across get_query_var() and related functions - so I tried this but still couldn't access the 'error' variable. After that, I output the value of $wp_query->query_vars and noticed that the 'error' variable exists, and is empty, regardless of whether the variable is set in the URL.

So, my question is, how can I check for the existence of the 'error' variable (preferably without having to parse the url - but I'll keep that as a backup plan). I should probably mention that I'm not usually a wordpress plugin developer so, if I'm being an idiot and missing something simple, please let me know!

/**** Edit **/

Here's a little bit of test code that simulates what I'm attempting to do. You'll see that I've attached handlers to the 'send_headers' action - this is where I really need to get the variable. The 'the_content' is there for comparison.

The code: https://gist.github.com/ryanlund/e644b10a9e50ae7b03e6 (it's on gist because it's pretty lengthy)

The log: https://gist.github.com/ryanlund/e0d685545a09eff93a83

I have trimmed the showAll section of the log to just the parts we're interested in. I ran the code with the following query string in the url: ?test=A%20Test&error=An%20error

/**************************** Final Solution *******************************/

Hi everyone, here is my final solution based on the ideas Tom gave me (see the accepted answer)

Basically, rather than still attempting to use a variable that is reserved (not cool wordpress!), I simply renamed it:

    add_action('init',function() { 
        if(preg_match('/^\/test-page\//',$_SERVER['REQUEST_URI'])){
            if(isset($_GET['error'])){
                /*call it something unlikely to be used elsewhere*/
                $_GET['error_mpp']=$_GET['error']; 
            }

            global $wp; 
            $wp->add_query_var('test');
            $wp->add_query_var('error_mpp');
        }
    });
  • Can you post some code? – Tom J Nowell Apr 24 '15 at 21:01
  • @TomJNowell Wrote a test and added it so you can see completely. – Ryan Lund Apr 24 '15 at 22:02
3

Your problem is that you're using Reserved Terms. error is a reserved term, you can't use it.

To get around this, you could intercept the request early, and do a redirect, changing the query parameters in the process. For example, handling a link get parameter:

add_action( 'wp' , 'reserved_term_intercept' );

function reserved_term_intercept(){
    global $wp_query;
    if( $wp_query->queried_object_id == 72 && isset( $_GET["link"] ) ){

        wp_redirect( get_permalink( 72 ) . "?newlink=" . urlencode( $_GET["link"] ) . "&nonce=" . wp_create_nonce( 'nonce-name' ) );
        exit;
    }
}

Then handle the new variable:

add_action( 'wp_head' , 'reserved_term_handling' );

function reserved_term_handling(){
    if( wp_verify_nonce( $_GET["nonce"], 'nonce-name' ) == TRUE && isset( $_GET["newlink" ) ){
        printf( '<a href="%s">Hello World!</a>', esc_url( urldecode( $_GET["newlink"] ) ) );
    }
}

wp_head may not be the most appropriate hook to run the code on, probably init instead

  • Excellent, thank you! I knew it would be something like this but couldn't really find anything about it - clearly I was using the wrong search terms haha. Anyway, I'll give it a whirl and report back soon :) – Ryan Lund Apr 25 '15 at 7:25
  • I have updated my question with my final solution. Thanks for all your help mate :) Ryan – Ryan Lund Apr 25 '15 at 14:31
  • I'd advise against your renaming solution unless you can insert it at the earliest possible time as the error var is still used by WP Core, and the init hook is quite late for such a change – Tom J Nowell Apr 25 '15 at 15:02
  • Hi again Tom, thanks for your advice. My reasons for putting it in the init were: Cleaner code as I was already using that hook and that, on codex.wordpress.org/Plugin_API/Action_Reference , it says the 'init' hook is called before the 'parse_query' hook so I figured it should be a safe place to mess about with the query vars? Anyway, following your advice, I found that 'registered_taxonomy' is the earliest hook I can use (muplugins_loaded didn't work), so I've stuck it on there. Should be ok there, right? Thanks, Ryan – Ryan Lund Apr 25 '15 at 18:22
  • Oh also, I wasn't overly keen on redirecting considering the number of redirects already in use during a oauth login haha - which is why I decided to just rename. – Ryan Lund Apr 25 '15 at 18:25

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.