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.
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 theinit
action - or an earlier one such asplugins_loaded
.http://example.com?action=verify_blah&code=xxxxxx
in the email; then in theinit
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 theaction
orcode
to theWP_Query
's public query args (which you can do via thequery_vars
filter).code
in a meta field of a post that I verify against. When I use theinit
hook and doget_post_meta
with theid
of the post I get no post. This is because at theinit
hook the WP class object is not yet set up. So what is better, hook into thewp
hook and get the post meta value like that, or keep using theinit
hook and inside it run anew WP Query
for the post with theid
to then check against the meta field value? I opt for thewp
hook. What do u think? Thxwp
would be the earliest action you can hook to, though most devs I see usetemplate_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 useget_post_meta( $_GET['post'], 'key', true )
in theinit
hook. (You can prettify the link, but that's another story.)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 theid
, it checks out with theinit
action hook now. Thx heaps!