0

In a from response email I want to send a link to with a unique url string attached to it. Clicking that link and visiting the website through that link should trigger a function. The way I thought to go about it is this.

Add a query argument that will hold a random string as a value. When the pre_get_posts action catches the query and the random link matches I can call a function. This below is very basic code.

esc_url( add_query_arg( 'action', 'random_verification_link' ) );

function add_query_vars_filter( $vars ) {
  $vars[] = 'action';
  return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );

function catch_verification( $query ) {
    if ( $query->is_main_query() ) {
    // var_export( $query );
    // var_export( $query->query );
    $action = $query->get( 'action' );
    // var_export( $action );
        if ( 'random_verification_link' === $action ) {
            wp_die( 'Link OK.. ' );
        } else {
            return;
        }
    }
}
add_action( 'pre_get_posts', 'catch_verification' );

The random string would have to be created and stored per form submit id and have time to die and die after having been visited. But that is for later.

For now, I am wondering if taking this approach is the right way? Can I, in fact, use the value of an array to store a random string for this use case? Is retrieving that value with add_query_var and pre_get_posts a good idea? Any help on getting me in the right direction is great.

The function is nothing to do with user logins or user roles, I simply want to see if I can get to send verification links that cause an action when visited. Like to learn this better, thx.

5
  • 1
    You basically use pre_get_posts only when you want to filter the posts result, for example on an archive page, to set the number of posts displayed per page be different than the default one (e.g. 10). If you want to verify a link from an email, you can do so via the init action - or an earlier one such as plugins_loaded.
    – Sally CJ
    Commented Oct 20, 2018 at 8:59
  • 1
    So for example, you could have http://example.com?action=verify_blah&code=xxxxxx in the email; then in the init hook, you can use $_GET['action'] and $_GET['code'] to capture the action name and the specific verification code. There's no need to add the action or code to the WP_Query's public query args (which you can do via the query_vars filter).
    – Sally CJ
    Commented Oct 20, 2018 at 9:00
  • Thx Sally, very useful to know! Now I have the code in a meta field of a post that I verify against. When I use the init hook and do get_post_meta with the id of the post I get no post. This is because at the init hook the WP class object is not yet set up. So what is better, hook into the wp hook and get the post meta value like that, or keep using the init hook and inside it run a new WP Query for the post with the id to then check against the meta field value? I opt for the wp hook. What do u think? Thx
    – lowtechsun
    Commented Oct 22, 2018 at 4:22
  • 1
    Yes, if you need to access the current queried object (e.g. a single post), wp would be the earliest action you can hook to, though most devs I see use template_redirect. Alternatively, you can include the post ID in the verification link (in the email) - http://example.com?action=verify_blah&code=123456&post=123, and use get_post_meta( $_GET['post'], 'key', true ) in the init hook. (You can prettify the link, but that's another story.)
    – Sally CJ
    Commented Oct 22, 2018 at 5:14
  • 1
    Yes, I did already use the id in the query array, have a url pretty similar to what you write, and yet again, did not know I could pull the meta field value through the $_GET global and the id, it checks out with the init action hook now. Thx heaps!
    – lowtechsun
    Commented Oct 22, 2018 at 7:02

0

Your Answer

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

Browse other questions tagged or ask your own question.