WordPress: Update user email address, or add a new user with only theme access

If you’re editing a website but only have theme access, and no access to the database, you can temporarily add the code below into your theme ‘front-page.php’ or other theme file to update either:

  • The existing first user’s email address
  • Add a new Administrator user

Remember to remove this code when your have finished your operations.

Update the existing user’s email address


<?php  
    /************************
     * Change an existing WordPress user email address
     ***********************/

    # If specific IP address, find from: https://whatismyipaddress.com/
    if ($_SERVER["REMOTE_ADDR"] == 'Your IP Here') {

        //View the site admin email
        echo get_bloginfo( 'admin_email' ); 


        //Find the first User - this is generally the Admin user which was created when WordPress was installed
        $blogusers = get_users( 'blog_id=1' );      //ID: 1

        // Array of WP_User objects.
        foreach ( $blogusers as $user ) {
            echo '<span>' . esc_html( $user->user_email ) . '</span>';
        }


        //Once the above code has displayed the first user's email address, you can un-comment ( /* and */) below to update the email address with your own.

        /*Update user email address*/
        /*
            //Update user ID 1. Replace 'youremailhere@domain.com' with your email address
            $user_id = wp_update_user( array( 'ID' => 1, 'user_email' => 'youremailhere@domain.com' ) );    

            if ( is_wp_error( $user_id ) ) {
                echo "User update error";
            } else {
                echo "User updated";
            }
        */
    }
?>

Add a new Administrator user


<?php  
    /************************
     * Add a new WordPress admin user
     ***********************/

    # If specific IP address, find from: https://whatismyipaddress.com/
    if ($_SERVER["REMOTE_ADDR"] == 'Your IP Here') {

        /*Add new admin user*/
            //New user details
            $username = 'yourusername';                     //Replace 'yourusername' with your username
            $password = 'secure password here';             //Replace 'secure password here' with your own secure password
            $email_address = 'youremailhere@domain.com';    //Replace 'youremailhere@domain.com' with your own email address

            //If user does not already exist
            if ( ! username_exists( $username ) ) {
                $user_id = wp_create_user( $username, $password, $email_address );      //Create user

                //If user created
                if ($user_id) {
                    $user = new WP_User( $user_id );                                    //Find created user's ID
                    $user->set_role( 'administrator' );                                 //Set role to 'administrator'
                    echo "User created";
                } else {
                    echo "User created error";
                }
            } else {
                echo "User already exists";
            }

    }
?>
There are no comments, be the first. Use the form below to add your comment

Post a Comment