1

I'm developing a plugin that at some point sends a verification email to the user. When the user clicks this link, the plugin goes to work with the verification code.

Currently, I've done this using a shortcode, but this requires the user (or the plugin) to create a page with this shortcode in it. Is it not possible to somehow catch the http://site.tld/?token=x url, execute the code, and display a thank you message without creating a page in which to execute the code?

  • With the right hook, you have full control over the content (template_redirect comes to mind). Do you need your thank you message to be in the site design & layout? – janh Jan 4 '18 at 9:30
  • ideally, yes, although I realise that may not be fully possible – bluppfisk Jan 4 '18 at 9:41
3

You can use the init hook to check the $_REQUEST or $_GET super-global variable for your token value and then redirect to pages that are present via WP Core:

  • home_url
  • wp_login_url

If you pass additional parameters to these URLs, such as token_valid=true you can use that along with conditional tags like is_front to conditionally display thank you message.

function wpse290114_init_validate() {
    if(isset($_REQUEST['token']) && !empty($_REQUEST['token'])){
        // validate the token and set $token_valid to 'true' or 'false'
        if($token_valid){
            wp_safe_redirect(add_query_arg( 'token_valid', $token_valid, home_url() ));
        } else{
            wp_safe_redirect(add_query_arg('token_valid', $token_valid, wp_login_url() );
        }

    }
}
add_action("init", "wpse290114_init_validate");

The position and display of the thank you message depends on the theme.

function wpse290114_thankyou(){
    if(isset($_REQUEST['token_valid']) && 'true' === $_REQUEST['token_valid'])){
      echo '<p class="message--thankyou">Thank you for validating.</p>';
    }
}
add_action("your_theme_hook", "wpse290114_thankyou");
  • Thanks Andrew, I think I get that. But so in order for it to fit with the theme, I need to figure out what the theme hook is, which I understand is different for every theme? – bluppfisk Jan 4 '18 at 9:46
  • 1
    Yep, different themes provide different hooks. Plugin devs often provide both a function and a shortcode for displaying these kind of things. People with the knowhow can call the function directly within theme template file and shortcodes can added via admin. Beyond that, your plugin could create it's own pages on activation so you can be sure they'll exist and know their post ID. – Andrew Jan 4 '18 at 10:17
  • great I think that works. Is it also possible to suppress any other code? E.g. prevent the homepage from being loaded if the hook is called? – bluppfisk Jan 4 '18 at 10:32
  • You could extend the wpse290114_init_validate function above called on init to call wp_die instead of redirecting if the token is not valid. This would be an extream action and it's probably better to provide people with options to request the validation token again or complete the validation another way. – Andrew Jan 4 '18 at 12:59
  • oh I meant when displaying the thank you message. You may not want any further homepage template to be shown after the thank-you message. – bluppfisk Jan 5 '18 at 9:57

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.