在WordPress中以编程方式创建管理员用户

在WordPress中以编程方式创建管理员用户

问题描述:

这是一个问答集:

最近我有一个客户来找我来建立一个新网站。他们也忘记了所有登录详细信息,但是他们确实具有FTP访问权限。

Recently I had a client come to me to build a new website. They had also forgotten all their login details, but they did have FTP access.

那么,如何通过代码创建管理员用户?

如果放在主题functions.php文件中,则会创建一个管理员用户。

This will create an admin user if placed in a themes functions.php file.

请根据需要更改前三个变量。

Please change the first three variables as needed.

/* 
 * Create an admin user silently
 */    

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