I'm doing some work with Tabulas to integrate more closely with a WordPress install. I needed to create a programmatic interface to automatically create users.

The best place seemed to be the XML-RPC interface. So, if anybody else wants this code, here it is:

Copy this function within the wp_xmlrpc_server class:

    /**
     * Create a user programmatically
     *
     * @since 2.2.0
     *
     * @param array $args Method parameters.
     * @return array
     */
    function custom_createAuthor($args) {
        $this->escape($args);
        
        $user_name =  $args[0];
        $user_password    = $args[1];
        $user_email    = $args[2];
        
        $user_id = wp_create_user($user_name, $user_password, $user_email);
        
        if ( is_wp_error( $user_id ) ) {
            return (new IXR_Error(500, __($user_id->get_error_message())));
        }
        return array(
            'username' => $user_name, 
            'userid' => $user_id, 
            'email' => $user_email, 
            'password' => $user_password
        );
    }

I then added the new method to the list of XML-RPC calls within the wp_xmlrpc_server() function (you'll see a huge list that looks similar to this one, just add it on any line, the order doesn't matter:

            // Extend WordPress API
            'custom.createAuthor'        => 'this:custom_createAuthor',

On your other script's side, just use the generic XML-RPC PHP library to do something like this:

	require_once('XML-RPC.php');
	
	$c = new xmlrpc_client('/xmlrpc.php', 'your-wordpress-site.com', 80); 
$f = new xmlrpcmsg( 'custom.createAuthor', array( new xmlrpcval('username', "string"), new xmlrpcval('password', "string"), new xmlrpcval('useremail@domain.com', "string") ) ); $result_struct = $c->send($f, 1); //set timeout // Check the results for an error if (!$result_struct->faultCode()) { // Get the results in a value-array $values = $result_struct->value(); // Compile results into PHP array $result_array = php_xmlrpc_decode($values); print '<pre>'.print_r($result_array, true).'</pre>'; } else { print $result_struct->faultString(); }

(By the way, XML-RPC is such a PITA... I should really use the Atom endpoint for WordPress, but I'm too lazy; I already had a bunch of code to bootstrap against the XML-RPC interface.)

Posted by roy on September 12, 2010 at 02:32 PM in Web Development | 1 Comments

Related Entries

Linked Entries

These are Tabulas entries which have linked to this particular entry.

Want to comment with Tabulas?. Please login.

R (guest)

Comment posted on September 12th, 2010 at 10:44 PM
Instead of editing a core WordPress file to add this new XML-RPC
method I'd recommend doing it via a plugin. Doing so is very easy, I
have an example at -
http://josephscott.org/archives/2008/11/adding-xml-rpc-methods-to-wordpress/

You may also want to look at the IXR XML-RPC library (that is what WP
uses and ships with), it takes care of some of those manual steps.