最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Pull GET parameter from URL in functions.php

programmeradmin0浏览0评论

I've followed the documentation from WordPress, but my functions.php file still is not getting the token (abcd) variable from the URL: /?token=abcd

My functions.php file includes the following:

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

...

add_filter( 'wpcf7_validate_text*', 'custom_password_reset_validation_filter', 10, 2 );
function custom_password_reset_validation_filter( $result, $tag ) {
    // Make POST request to change password
            $token = get_query_var( 'token', "fk" );
            write_log("TKN: {$token}");
    }

    return $result;
}

For some reason, get__query_var always returns fk instead of abcd.

I've followed the documentation from WordPress, but my functions.php file still is not getting the token (abcd) variable from the URL: https://example/reset-password/?token=abcd

My functions.php file includes the following:

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

...

add_filter( 'wpcf7_validate_text*', 'custom_password_reset_validation_filter', 10, 2 );
function custom_password_reset_validation_filter( $result, $tag ) {
    // Make POST request to change password
            $token = get_query_var( 'token', "fk" );
            write_log("TKN: {$token}");
    }

    return $result;
}

For some reason, get__query_var always returns fk instead of abcd.

Share Improve this question asked Aug 21, 2019 at 3:56 Jameson RaderJameson Rader 11 bronze badge 2
  • If wpcf7_validate_text is handled in a separate AJAX request, then the token parameter is not going to exist. – Jacob Peattie Commented Aug 21, 2019 at 5:23
  • But then how can I get the GET parameter in my Contact Form 7 validation? – Jameson Rader Commented Aug 22, 2019 at 1:15
Add a comment  | 

2 Answers 2

Reset to default 0

Contact Form 7 provides a recipe for Validation as a Filter which looks very much like what you are trying to do.

Adapting their example to your code, I believe the following should work for you:

add_filter( 'wpcf7_validate_text*', 'custom_password_reset_validation_filter', 10, 2 );
function custom_password_reset_validation_filter( $result, $tag ) {        
    if ('token'==$tag->name){
        $token = isset($_POST['token']) ? $_POST['token'] : "fk";
        write_log("TKN: {$token}");
    }

    return $result;
}

You can try

$token = $_REQUEST['token']
发布评论

评论列表(0)

  1. 暂无评论