10

コードを介して管理者ロールを持つ新しいユーザーを追加する必要があり、次のコードが見つかりました:

add_action('init', 'add_user');
function add_user() {
    $username = 'username123';
    $password = 'pasword123';
    $email = 'drew@example.com';

    // Create the new user
    $user_id = wp_create_user( $username, $password, $email );

    // Get current user object
    $user = get_user_by( 'id', $user_id );

    // Remove role
    $user->remove_role( 'subscriber' );

    // Add role
    $user->add_role( 'administrator' );
}

ここ

しかし、に追加するとfunctions.php、次のエラーが発生しました。

 Fatal error: Call to a member function remove_role() 
 on a non-object in ..../functions.php on line ...

私もこのコードを試しました:

 function fb_wp_insert_user() {
$user_data = array(
'ID' => '',
'user_pass' => wp_generate_password(),
'user_login' => 'dummy',
'user_nicename' => 'Dummy',
'user_url' => '',
'user_email' => 'dummy@example.com',
'display_name' => 'Dummy',
'nickname' => 'dummy',
'first_name' => 'Dummy',
'user_registered' => '2010-05-15 05:55:55',
'role' => get_option('default_role') // Use default role or another role, e.g. 'editor'
);
$user_id = wp_insert_user( $user_data );
}
add_action( 'admin_init', 'fb_wp_insert_user' );

デフォルトの役割を に変更しましadminstratorたが、ユーザーを参照すると、このユーザーには役割がありませんでした。

4

4 に答える 4

13

これはあなたのエラーです

致命的なエラー: 行 ..../functions.php の非オブジェクトでメンバ関数 remove_role() を呼び出します ...

これは$user->remove_role( 'subscriber' );コードが原因であり、次のコードを使用して新しいユーザーを取得する場合、

$user = get_user_by( 'id', $user_id );

WP_Userオブジェクトを返していません。したがって、オブジェクト以外でメソッドを呼び出すと、このエラーが表示されIDます。

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

ユーザーを正常に作成できなかった可能性があります。この場合、戻り値は次のようになりますobjectCodex

成功すると、この関数は作成されたユーザーのユーザー ID を返します。失敗した場合 (ユーザー名または電子メールが既に存在する場合)、関数はこれらの可能な値とメッセージと共にエラー オブジェクトを返します。

empty_user_login、空のログイン名でユーザーを作成できません。

existing_user_login, このユーザー名は既に登録されています.

existing_user_email, このメールアドレスは既に登録されています.

SO、ユーザーを作成するときは、まずユーザーが存在するかどうかを確認します

add_action('init', 'add_my_user');
function add_my_user() {
    $username = 'username123';
    $email = 'drew@example.com';
    $password = 'pasword123';

    $user_id = username_exists( $username );
    if ( !$user_id && email_exists($email) == false ) {
        $user_id = wp_create_user( $username, $password, $email );
        if( !is_wp_error($user_id) ) {
            $user = get_user_by( 'id', $user_id );
            $user->set_role( 'administrator' );
        }
    }
}

また、ロールを削除して追加する必要はありません。set_role($role)はユーザーの以前のロールを削除し、ユーザーに新しいロールを割り当てます。wp create userget user by onの詳細をお読みくださいCodex。また、wp_generate_password()をチェックして、プレーンテキストの代わりに安全なパスワードを使用してください。

アップデート :

add_userは WordPress の関数なので、名前をadd_my_user.

于 2013-11-07T20:11:30.063 に答える
6

wp_create_user()が実際にユーザーを作成したことを確認します。

add_action('init', 'add_user');
function add_user() {
    $username = 'username123';
    $password = 'pasword123';
    $email = 'drew@example.com';

    $user = get_user_by( 'email', $email );
    if( ! $user ) {

        // Create the new user
        $user_id = wp_create_user( $username, $password, $email );
        if( is_wp_error( $user_id ) ) {
            // examine the error message
            echo( "Error: " . $user_id->get_error_message() );
            exit;
        }

        // Get current user object
        $user = get_user_by( 'id', $user_id );
    }

    // Remove role
    $user->remove_role( 'subscriber' );

    // Add role
    $user->add_role( 'administrator' );
}

編集済み: 以下のコメントによると、ユーザーは既に作成されているようです。それを確認するためにコードを更新しました。(本質的に、ユーザーがまだ存在しない場合は作成されます。)

参考文献

于 2013-11-07T20:00:13.723 に答える