小编Dar*_*rio的帖子

Wordpress Auth + Nonce + Ajax + 无模板 - Cookie nonce 无效

我了解创建自己的模板时使用随机数的过程。但是我正在开发一个 ReactJS 应用程序,它只使用 Wordpress REST API 来提取数据,所以用户永远不会访问 index.php,而是对 WP Rest Api 进行 Ajax 调用。

现在我无法让 nonce 工作。这是我到目前为止所做的:

我添加了以下端点:

register_rest_route('frontend', '/customer/', array(
    'methods' => 'GET',
    'callback' => 'get_customer'
));

register_rest_route('frontend', '/customer/', array(
    'methods' => 'POST',
    'callback' => 'create_user_and_login'
));
Run Code Online (Sandbox Code Playgroud)

这些是我的功能:

function get_customer()
{
    return get_current_user_id();
}


function create_user_and_login(){
    // dummy data for testing
    $credentials = ['user_login' => 'mail@mymail.de', 'user_password' => 'XXXX', 'remember' => true];

    // create a new user/customer via woocommerce
    $customerId = wc_create_new_customer($credentials['user_login'], $credentials['user_login'], $credentials['user_password']);
    if(is_a($customerId,'WP_Error')) {
        return $customerId;
    } …
Run Code Online (Sandbox Code Playgroud)

authentication ajax rest wordpress nonce

7
推荐指数
1
解决办法
1855
查看次数

标签 统计

ajax ×1

authentication ×1

nonce ×1

rest ×1

wordpress ×1