最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Change user metadata on registration (show_admin_bar_front = false)

programmeradmin0浏览0评论

This is my first contact with WP Development, and I'm trying to not display the WP admin bar after the registration. Since it's metadata called 'show_admin_bar-front', I'm updating that right after creating the user.

 if(count($error) == 0) {
            wp_create_user( $username, $password, $email);
            $user_id = get_current_user_id();
            update_user_meta($user_id, 'show_admin_bar_front', 'false');
            echo "User Created Successfully!";
            exit();
        }

As you can see, in the chunk above, I try to get the user ID right after creating the user and then updating the pointed metadata ('true' as default). Probably I'm doing something pretty stupid. Could someone enlighten me? Bellow, the full code.

 <?php
    /*
    Template Name: Registration
    */
    get_header();
    global $wpdb;

    if($_POST) {

        $username = $wpdb->escape($_POST['txtUsername']);
        $email = $wpdb->escape($_POST['txtEmail']);
        $password = $wpdb->escape($_POST['txtPassword']);
        $ConfPassword = $wpdb->escape($_POST['txtConfirmPassword']);


    $error = array();
        if(strpos($username, '') !==FALSE) {
            $error['username_space'] = "Username has space!";
        }

        if(empty($username)) {
            $error['username_empty'] = "Username is empty!";
        }

        if(username_exists($username)) {
            $error['username_exists'] = "Username already exists!";
        }

        if(!is_email($email)) {
            $error['email_valid'] = "Please, add a valid e-mail.";
        }

        if(email_exists($email)) {
            $error['email_existence'] = "This e-mail is already registered.";
        }

        if(strcmp($password, $ConfPassword) !==0) {
            $error['password'] = "Password don't match!";
        }

        if(count($error) == 0) {
            wp_create_user( $username, $password, $email);
            $user_id = get_current_user_id();
            update_user_meta($user_id, 'show_admin_bar_front', 'false');
            echo "User Created Successfully!";
            exit();
        } else {
            print_r($error);
        }
    }

    ?>

    <form method="post">

        <p>
            <label>Username</label>
            <div>
                <input type="text" id="txtUsername" name="txtUsername" placeholder="Username"/>
            </div>
        </p>

        <p>
            <label>Email</label>
            <div>
                <input type="email" id="txtEmail" name="txtEmail" placeholder="Email"/>
            </div>
        </p>

        <p>
            <label>Password</label>
            <div>
                <input type="password" id="txtPassword" name="txtPassword" placeholder="Password"/>
            </div>
        </p>

        <p>
            <label>Confirm Password</label>
            <div>
                <input type="password" id="txtConfirmPassword" name="txtConfirmPassword" placeholder="Confirm Password"/>
            </div>
        </p>

        <input type="submit" name="btnSubmit"/>

    </form>

This is my first contact with WP Development, and I'm trying to not display the WP admin bar after the registration. Since it's metadata called 'show_admin_bar-front', I'm updating that right after creating the user.

 if(count($error) == 0) {
            wp_create_user( $username, $password, $email);
            $user_id = get_current_user_id();
            update_user_meta($user_id, 'show_admin_bar_front', 'false');
            echo "User Created Successfully!";
            exit();
        }

As you can see, in the chunk above, I try to get the user ID right after creating the user and then updating the pointed metadata ('true' as default). Probably I'm doing something pretty stupid. Could someone enlighten me? Bellow, the full code.

 <?php
    /*
    Template Name: Registration
    */
    get_header();
    global $wpdb;

    if($_POST) {

        $username = $wpdb->escape($_POST['txtUsername']);
        $email = $wpdb->escape($_POST['txtEmail']);
        $password = $wpdb->escape($_POST['txtPassword']);
        $ConfPassword = $wpdb->escape($_POST['txtConfirmPassword']);


    $error = array();
        if(strpos($username, '') !==FALSE) {
            $error['username_space'] = "Username has space!";
        }

        if(empty($username)) {
            $error['username_empty'] = "Username is empty!";
        }

        if(username_exists($username)) {
            $error['username_exists'] = "Username already exists!";
        }

        if(!is_email($email)) {
            $error['email_valid'] = "Please, add a valid e-mail.";
        }

        if(email_exists($email)) {
            $error['email_existence'] = "This e-mail is already registered.";
        }

        if(strcmp($password, $ConfPassword) !==0) {
            $error['password'] = "Password don't match!";
        }

        if(count($error) == 0) {
            wp_create_user( $username, $password, $email);
            $user_id = get_current_user_id();
            update_user_meta($user_id, 'show_admin_bar_front', 'false');
            echo "User Created Successfully!";
            exit();
        } else {
            print_r($error);
        }
    }

    ?>

    <form method="post">

        <p>
            <label>Username</label>
            <div>
                <input type="text" id="txtUsername" name="txtUsername" placeholder="Username"/>
            </div>
        </p>

        <p>
            <label>Email</label>
            <div>
                <input type="email" id="txtEmail" name="txtEmail" placeholder="Email"/>
            </div>
        </p>

        <p>
            <label>Password</label>
            <div>
                <input type="password" id="txtPassword" name="txtPassword" placeholder="Password"/>
            </div>
        </p>

        <p>
            <label>Confirm Password</label>
            <div>
                <input type="password" id="txtConfirmPassword" name="txtConfirmPassword" placeholder="Confirm Password"/>
            </div>
        </p>

        <input type="submit" name="btnSubmit"/>

    </form>
Share Improve this question asked May 28, 2020 at 14:11 igortpigortp 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you want to update meta on registration like this I think a better option would be to use the register_new_user hook with add_action. https://developer.wordpress/reference/hooks/register_new_user/

So something like:


add_action('register_new_user','my_update_meta',10,1);


function my_update_meta($user_id){

 update_user_meta($user_id, 'show_admin_bar_front', false);


}

If in doubt, hooks/filters/actions tend to be the best option for WordPress work.

发布评论

评论列表(0)

  1. 暂无评论