PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 5, 2022

[FIXED] Wordoress How to Capture get variable in url during login

 February 05, 2022     php, wordpress     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing