Wordpress : Call wp_create_user function from external?

≡放荡痞女 提交于 2020-01-03 05:26:07

问题


In Wordpress, i want to create New Users from external. I've found this is function in wordpress:

wp_create_user( $username, $password, $email );

So how can i run this function from external call please?

I mean, how to run this function from either:

  • Via simple URL with GET, like: www.example.com/adduser/?username=james&password=simpletext&email=myemail
  • Via cURL with POST

.. from external website.


回答1:


You may try this but also make sure that the listening url has a handler to handle the request in your WordPress end (using GET methof)

function curlAdduser($strUrl)
{
    if( empty($strUrl) )
    {
        return 'Error: invalid Url given';
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $strUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    $return = curl_exec($ch);
    curl_close($ch);
    return $return;
}

Call the function from external site :

curlAdduser("www.example.com/adduser?username=james&password=simpletext&email=myemail");

Update : using POST method

function curlAdduser($strUrl, $data) {
    $fields = '';
    foreach($data as $key => $value) { 
        $fields .= $key . '=' . $value . '&'; 
    }
    $fields = rtrim($fields, '&');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $strUrl);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    $return = curl_exec($ch);
    curl_close($ch);
    return $return;
}

Call the function with data

$data = array(
    "username" => "james",
    "password" => "simpletext",
    "email" => "myemail"
);
curlAdduser("www.example.com/adduser", $data);


来源:https://stackoverflow.com/questions/19131483/wordpress-call-wp-create-user-function-from-external

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!