Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

im creating a custom class to manage users in wordpress and chamilo but this is not the point.

Class looks like this:

class Platform
{
    private $security_key = '..';
    private $secret_key;
    private $firstname;
    private $lastname;
    private $status;
    private $email;
    private $login;
    private $password;
    private $encrypt_method = "md5";
    private $original_user_id_name = "uid";
    private $original_user_id_value;

    public function __construct()
    {
        $arguments = func_get_args();
        if(!empty($arguments))
            foreach($arguments[0] as $key => $property)
            if(property_exists($this, $key))
            $this->{$key} = $property;
    }
    /*
     * Do autoryzacji z chamilo potrzeba IP clienta + auth_code.
    * Z tych danych jest tworzyony API_KEY potrzeby podczas komunikacje z platformą.
    * Funkcje sprawdzają akutalne IP oraz auth_code platformy i generują API_KEY
    */
    public function check_client_ip()
    {
        $ip = trim($_SERVER['REMOTE_ADDR']);
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            list($ip1,$ip2) = split(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
            $ip = trim($ip1);
        }
        return $ip;
    }
    public function generate_secret_key()
    {
        $ip_address = $this->check_client_ip();
        $secret_key = sha1($ip_address.$this->security_key);
        return $secret_key;
    }
    /*
     * Dodawanie użytkownika w Chamilo
    */
    public function add_user_to_chamilo()
    {
        $params = array(
                'firstname'                 => $this->firstname,
                'lastname'                  => $this->lastname,
                'status'                    => '5', // 5 STUDENT - 1 TEACHER - zawsze ustawiamy na studenta - konta administracyjne będziemy tworzyć ręcznie
                'email'                     => $this->email,
                'loginname'                 => $this->login,
                'password'                  => $this->password, // encrypted using sha1
                'encrypt_method'            => 'md5',
                'language'                  => 'Polish',
                'official_code'             => '',
                'phone'                     => '',
                'expiration_date'           => '0000-00-00',
                'original_user_id_name'     => 'uid', // the extra user field that will be automatically created in the user profile see: main/admin/user_fields.php
                'original_user_id_value'    => $this->wp_user_id, // third party user id
                'secret_key'                => $this->generate_secret_key(),
        );
        echo "<pre>";
        print_r($params);
        echo "</pre>";

        //$client = new SoapClient('http://....pl/registration.soap.php?wsdl');
        //$response = $client->WSCreateUserPasswordCrypted($params);
    }
    /*
     * Dodawanie użytkownika w Wordpress
    */
    public function add_user_to_wordpress()
    {
        $params = array(
                'ID'                        => '',
                'first_name'                => $this->firstname,
                'last_name'                 => $this->lastname,
                'user_email'                => $this->email,
                'user_login'                => $this->login,
                'user_pass'                 => $this->password,
                'role'                      => 'subscriber'
        );
        echo "<pre>";
        print_r($params);
        echo "</pre>";
        wp_insert_user($params);
    }
}

This is how i initialize the class in wordpress page (page-adduser.php)

$p = new Platform(array(
        'firstname'                 => 'John',
        'lastname'                  => 'Doe',
        'email'                     => 'test@example.com',
        'login'                     => 'test',
        'password'                  => 'somepass'

        )
);

Then i run public function add_user_to_wordpress

$p-> add_user_to_wordpress();

So it prints data from $params array but it seems this is not running wp_insert user.

Data printed:

Array
(
    [ID] => 
    [first_name] => John
    [last_name] => Doe
    [user_email] => test@example.com
    [user_login] => test
    [user_pass] => somepass
    [role] => subscriber
)

Can You tell me how to runn wordpress functions inside my custom class.

BTW class i loaded to function.php file in theme as a first line.

share|improve this question
    
It seems that wp_insert_post is working just not accepting values like $this->firstname etc. –  Charles Kłyciński Dec 29 '13 at 10:37
    
please edit the question to add info instead of commenting on it. –  Mark Kaplun Dec 29 '13 at 14:59
1  
Do you have debugging enabled? How and where do you use this class? –  s_ha_dum Dec 29 '13 at 15:56

1 Answer 1

In addition to the debugging advice from @s_ha_dum, you could try to catch the possible WP_Error thrown by wp_insert_user():

$user_id = wp_insert_user($params);

if ( is_wp_error( $user_id ) )
{
   echo $user_id->get_error_message();
}
else
{
    printf( 'User created with user_id = %d', $user_id );
}

Maybe the user already exists?

You could also use email_exists() and username_exists() to check.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.