我正在尝试在我的 WordPress 网站上实现 JWT 身份验证,并允许 React Native 应用程序注册新用户。
我想添加自定义用户元,但它无法正常工作。
然后我向 axios 请求了以下文档:
const options = {
method: 'POST',
url: 'XXX',
params: {
rest_route: '/simple-jwt-login/v1/users',
email: email,
password: password,
AUTH_KEY: 'XXX',
user_login: username,
display_name: imeIPrezime.split(" ")[0],
first_name: imeIPrezime.split(" ")[0],
user_meta:`{”tel”:"${tel}", ”adresa”:"${adresa}"}`
},
headers: {
cookie: 'PHPSESSID=XXX',
'User-Agent': 'insomnia/8.6.1'
}
};
console.log(options.params.user_meta);
axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error.response.data);
});
我还尝试通过 WordPress 中的插件添加多种内容 首先我尝试了这个
// Register custom user meta fields
function NKWP_register_custom_user_meta_fields() {
register_meta('user', 'tel', [
'type' => 'string',
'description' => 'Custom Field Description',
'single' => true,
'show_in_rest' => true, // Expose field in REST API
]);
register_meta('user', 'adresa', [
'type' => 'string',
'description' => 'Custom Field Description',
'single' => true,
'show_in_rest' => true, // Expose field in REST API
]);
}
add_action('init', 'NKWP_register_custom_user_meta_fields');
// Expose custom user meta field in REST API
function NKWP_expose_tel() {
register_rest_field('user', 'tel', [
'get_callback' => 'NKWP_get_tel',
'schema' => null,
]);
}
function NKWP_get_tel($user, $request) {
return get_user_meta($user['id'], "tel", true);
}
add_action('rest_api_init', 'NKWP_expose_tel');
// Expose custom user meta field in REST API
function NKWP_expose_adresa() {
register_rest_field('user', 'adresa', [
'get_callback' => 'NKWP_get_adresa',
'schema' => null,
]);
}
function NKWP_get_adresa($user, $request) {
return get_user_meta($user['id'], "adresa", true);
}
add_action('rest_api_init', 'NKWP_expose_adresa');
add_action('rest_api_init', 'NKWP_expose_tel');
然后我尝试只注册密钥
// Register custom user meta fields
function NKWP_register_custom_user_meta_fields() {
register_meta('user', 'tel', [
'type' => 'string',
'description' => 'Custom Field Description',
'single' => true,
'show_in_rest' => true, // Expose field in REST API
]);
register_meta('user', 'adresa', [
'type' => 'string',
'description' => 'Custom Field Description',
'single' => true,
'show_in_rest' => true, // Expose field in REST API
]);
}
add_action('init', 'NKWP_register_custom_user_meta_fields');
无论使用其中任何一个还是都不使用,注册都会失败。
唯一的区别是当我激活插件端代码时我得到这个响应
{
"id": 102,
"username": "xxx",
"name": "xxx",
"first_name": "xxx",
"last_name": "",
"email": "xxx",
"url": "",
"description": "",
"link": "xxx",
"locale": "en_US",
"nickname": "xxx",
"slug": "xxx",
"roles": [
"contributor"
],
"registered_date": "2024-04-10T20:30:06+00:00",
"capabilities": {
"edit_posts": true,
"read": true,
"level_1": true,
"level_0": true,
"delete_posts": true,
"contributor": true
},
"extra_capabilities": {
"contributor": true
},
"avatar_urls": {
"24": "https:\/\/secure.gravatar.com\/avatar\/9e725502ed014083cb676aceb2c79f21?s=24&d=mm&r=g",
"48": "https:\/\/secure.gravatar.com\/avatar\/9e725502ed014083cb676aceb2c79f21?s=48&d=mm&r=g",
"96": "https:\/\/secure.gravatar.com\/avatar\/9e725502ed014083cb676aceb2c79f21?s=96&d=mm&r=g"
},
"meta": {
"persisted_preferences": [],
"tel": "",
"adresa": ""
},
"_links": {
"self": [
{
"href": "xxx"
}
],
"collection": [
{
"href": "xxx"
}
]
}
}
并且没有区别是在元字段中
"meta": {
"persisted_preferences": [],
},
该问题最近已得到解决。只需更新插件即可正常工作。