简体   繁体   English

如何在 wordpress function php 中保存 cookie?

[英]How do I save a cookie in wordpress function php?

I want to save the user id in a cookie after registering them in a wordpress function: however, the function below doesnt save the cookie.我想在 wordpress function 中注册用户 id 后将其保存在 cookie 中:但是,下面的 function 不保存 cookie。 anything i'm missing here?我在这里缺少什么?

function register_user_form() {
    $user_login = $_POST['currentUserName'];
    $user_email = $_POST['currentUserEmail'];
    $user_pass = $_POST['currentUserPassword'];
    $userdata = compact( 'user_login', 'user_email', 'user_pass' );
    $user_id = wp_insert_user($userdata);

    $cookie_name = "userid";
    $cookie_value = $user_id;
    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");

    wp_die();

}
add_action('wp_ajax_register_user_form', 'register_user_form');
add_action('wp_ajax_nopriv_register_user_form', 'register_user_form');

Cookies are strings. Cookies 是字符串。 The return value of wp_insert_user() is an integer. wp_insert_user()的返回值为 integer。 You need to convert that to a string first before trying to set the cookie or it could fail.在尝试设置 cookie 之前,您需要先将其转换为字符串,否则可能会失败。 Try:尝试:

function register_user_form() {
    $user_login = $_POST['currentUserName'];
    $user_email = $_POST['currentUserEmail'];
    $user_pass = $_POST['currentUserPassword'];
    $userdata = compact( 'user_login', 'user_email', 'user_pass' );
    $user_id = wp_insert_user($userdata);

    $cookie_name = "userid";
    $cookie_value = strval($user_id); // Convert to a string
    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");

    wp_die();

}
add_action('wp_ajax_register_user_form', 'register_user_form');
add_action('wp_ajax_nopriv_register_user_form', 'register_user_form');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM