Issue
I'm creating an application where I have to save some get variable during login in WordPress, I have tried to use the wp_login
hook with no success, I have also used wp_authenticate
to capture the get variable among other methods with no success.
To make it more clear this is how I'm sending my get variable
https://localhost:8080/wp-login.php?wper=some_value
or https://localhost:8080/sign-in/?wper=some_value
Now what I need is to save get variable $_GET['wper']
in the database upon login, or rather upon clicking the login button.
please note my website uses two logins form, one is a custom login form from a plugin and the other one is the native wordpress login which is wp_login.php
the following is my code and my trial
function your_function( $username, $password ) {
if( is_email( $username ) ){
$user = get_user_by( 'email', $username );
} else {
$user = get_user_by( 'login', $username );
}
if( !$user ){
} else {
update_user_meta( $user->ID, '_wper_data', $_GET['wper'], '' );
}
}
add_action('wp_authenticate', 'your_function', 10, 2);
Solution
The $_GET is not getting passed with the form or via URL.
So one method would be to add it to the form submission.
add_action('wp_authenticate', 'your_function', 10, 1);
function your_function( $username ) {
if( is_email( $username ) ){
$user = get_user_by( 'email', $username );
} else {
$user = get_user_by( 'login', $username );
}
if( $user && isset( $_POST['_wper_data'] ) ){
update_user_meta( $user->ID, '_wper_data', $_POST['_wper_data'], '' );
}
}
add_action( 'login_form', 'custom_hidden_login_field', 10, 0 );
function custom_hidden_login_field() { ?>
<input type="hidden" name="_wper_data" value="<?php echo $_GET['wper']; ?>" />
<?php
}
Answered By - Mark Truitt
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.