在自定义类中调用wp_INSERT_USER无效

时间:2013-12-29 作者:Charles Kłyciński

我正在创建一个自定义类来管理wordpress和chamilo中的用户,但这不是重点。

类如下所示:

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);
    }
}
这是我在wordpress页面(page adduser.php)中初始化类的方式

$p = new Platform(array(
        \'firstname\'                 => \'John\',
        \'lastname\'                  => \'Doe\',
        \'email\'                     => \'[email protected]\',
        \'login\'                     => \'test\',
        \'password\'                  => \'somepass\'

        )
);
然后我运行公共函数add\\u user\\u to\\u wordpress

$p-> add_user_to_wordpress();
所以它从$params数组打印数据,但似乎它没有运行wp\\u insert user。

打印数据:

Array
(
    [ID] => 
    [first_name] => John
    [last_name] => Doe
    [user_email] => [email protected]
    [user_login] => test
    [user_pass] => somepass
    [role] => subscriber
)
你能告诉我如何在我的自定义类中运行wordpress函数吗。

BTW类i加载到函数。php文件以主题作为第一行。

1 个回复
SO网友:birgire

除了@s\\u ha\\u dum提供的调试建议之外,您还可以尝试捕获可能的WP_Error 被丢弃的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 );
}
可能用户已经存在?

您也可以使用email_exists()username_exists() 检查。

结束

相关推荐

users and usermeta table

每个用户在usermeta表中也必须有条目,还是可选的?最近,我遇到了一个wordpress数据库,它在用户表中有数千个用户,但只有少数用户在usermeta表中有条目。我想知道在什么情况下会发生这种情况,因为这些条目看起来不像是手动从数据库中删除的。