【问题标题】:Store fields in the database and read the same in logging在数据库中存储字段并在日志记录中读取相同的字段
【发布时间】:2017-10-24 14:18:11
【问题描述】:

在将 wordpress 与另一台服务器连接后,我从另一台服务器获得了一个特殊的 ID,但它与 wordpress 用户的 ID 相关,我如何将第二个 ID 保存到基础,并将其提交给登录后?

    $user_id = $arr['id'];

if($user_id){
     $_SESSION['user_id'] = $user_id;


     /* Attempt MySQL server connection. Assuming you are running MySQL
     server with default setting (user 'root' with no password) */
     $link = mysqli_connect("localhost", "root", "", "localm");

     // Check connection
     if($link === false){
         die("ERROR: Could not connect. " . mysqli_connect_error());
     }
      //
     // Attempt insert query execution
     $sql = 'INSERT INTO wp_users (user_id) VALUES ("'.$user_id=$_SESSION['user_id'].'");';
     if(mysqli_query($link, $sql)){
         echo "Records inserted successfully.";
     } else{
         echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
     }
     // Close connection
     mysqli_close($link);

我目前正在使用 $ _SESSION 并且此 ID 在该浏览器中保存,但是当它被签名时它就消失了。这就是为什么我必须将其保留在底座中的原因。在这种情况下,哪种重置最容易?谢谢大家

【问题讨论】:

    标签: php database wordpress


    【解决方案1】:

    如果我理解了这个问题,这对你有用.. 您将另一个 ID 保存到 add_user_meta,然后使用当前 Wordpress 用户 ID 和 get_user_meta 调用它

    function save_ID( $user )
    {
       //get current user details
       if( !$user ) { $user = wp_get_current_user(); }
       
       //check if session registered
       if( !isset($_SESSION['user_id']) ){ return; }
    
       //store the 2nd ID to user_meta
       $user_id = $user->ID;
       $another_id = esc_html( esc_js( $_SESSION['user_id'] ) );
       add_user_meta( $user_id, 'user_2nd_id', $another_id);
    }
    
    add_action( 'wp_login', 'save_ID');

    希望对你有所帮助。 谢谢。

    【讨论】:

    • 嗨,首先感谢公告,我需要保留这个我的 id 并在他登录时使用 wp 用户的 id 调用.. 我在哪里可以包含此代码?
    猜你喜欢
    • 1970-01-01
    • 2022-12-15
    • 2011-07-17
    • 2017-02-21
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 2022-10-20
    相关资源
    最近更新 更多