WordPress API 传递电子邮件参数

如何接收来自休息路线的电子邮件?我想用 register_rest_route 注册一个用户。我需要来自 API 路由的三个参数(用户名、密码、电子邮件)。电子邮件参数的真正正则表达式是什么?


add_action('rest_api_init', function () {

register_rest_route('user/v2', 'register/(?P<name>[a-zA-Z0-9-]+)/(?P<password>[a-zA-Z0-9-]+)/(?P<email>[a-zA-Z0-9-]+)', [

    'method' => 'PUT',

    'callback' => 'user_create_callback',

    'args' => [

        'nema', 'email', 'password'

    ]

    

]);

});


function user_create_callback($args)

{

    //smoe validation for entries here

    wp_create_user( $args['name'], $args['password'], $args['email'] );

    return ['status' => 'user created successfuly'];

}


慕姐4208626
浏览 168回答 1
1回答

红颜莎娜

据我所知,您的代码中有几个问题。你有一个错字args-&nbsp;nema=>name您正在使用PUTHTTP 方法,我们通常将其用于更新,在创建 REST 对象时,我们通常使用POST.&nbsp;(您也可以使用WP_REST_SERVER::CREATABLE常量您通过查询字符串而不是使用请求正文(表单数据)传递参数要验证电子邮件地址,您只需使用 WordPressis_email()功能。将它们放在一起应该看起来像这样:<?phpadd_action('rest_api_init', function () {&nbsp; &nbsp; register_rest_route('user/v2', 'register', [&nbsp; &nbsp; &nbsp; &nbsp; 'method' => WP_REST_SERVER::CREATABLE,&nbsp; &nbsp; &nbsp; &nbsp; 'callback' => 'user_create_callback',&nbsp; &nbsp; &nbsp; &nbsp; 'args' => array (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => array (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'sanitize_callback' => 'sanitize_text_field'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'password' => array (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'sanitize_callback' => 'sanitize_text_field'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'email' => array (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'validate_callback' => 'is_email'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; ]);});function user_create_callback($args){&nbsp; &nbsp; wp_create_user( $args['name'], $args['password'], $args['email'] );&nbsp; &nbsp; return ['status' => 'user created successfuly'];}?>
打开App,查看更多内容
随时随地看视频慕课网APP