夏期劇場
夏期劇場

Reputation: 18337

Wordpress : Call wp_create_user function from external?

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:

.. from external website.

Upvotes: 1

Views: 1675

Answers (1)

The Alpha
The Alpha

Reputation: 146219

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);

Upvotes: 1

Related Questions