• I am trying to implement a register form where I have a role selection radio button with choices”Role1″ & “Role2” and other form elements

    It doesn’t update the role for the registered user. It just adds None to the role after the registration is complete, but in essence I need the role which was selected to be saved when saving the username and other fields.

    Here is what I have so far –

    I have a registration form with fields which has radio choices like –

    <label>
         <input type="radio" name="user_role" id="Role1<?php $template->the_instance(); ?>_r" class="input"
         <?php if (isset($_POST['user_role']) && $_POST['user_role'] == 'Role1' ) echo 'checked="checked"'; ?> value="Role1"/>Role1
    </label>
    <label>
         <input type="radio" name="user_role" id="Role2<?php $template->the_instance(); ?>" class="input"
         <?php if (isset($_POST['user_role']) && $_POST['user_role'] == 'Role2' ) echo 'checked="checked"'; ?> value="Role2"/>Role2
    </label>

    This is what I have in another page that will process the incoming data.
    The error module works fine, ie. It only shows the error message when I am not checking any role in my registration page.

    <?php
    function tml_registration_errors( $errors ) {
    	if ( !isset( $_POST['user_role'] ) )
            $errors->add('empty_user_role', '<strong>ERROR</strong>: Please select a role.');
        return $errors;
    }
    add_filter( 'registration_errors', 'tml_registration_errors' );

    Saving module below doesn’t take effect and I am having issues. For debugging purpose I have the alert setup but that doesn’t alert show the alert box either, it is just bypassed and “None” is added to the role column in the dashboard view of user list.

    function tml_user_register( $user_id ) {
        $user = new WP_User( $user_id );
    
        if ($_POST['user_role'] = 'Role1'){
            echo "<script>alert('" . $_POST['user_role'] . "');</script>";
            $user->remove_role( 'Default Role' );
            $user->set_role( 'Role1' );
            update_user_meta( $user_id, 'role', $_POST['user_role'] );
    	unset( $user );
       }else{
            echo "<script>alert('" . $_POST['user_role'] . "');</script>";
            $user->remove_role( 'Default Role' );
            $user->add_role( 'Role1' );
            $user->set_role( $_POST['user_role'] ) ;
            update_user_meta( $user_id, 'role', $_POST['user_role'] );
    	unset( $user );
        }
    }
    add_action( 'user_register', 'tml_user_register' );
        ?>

    Please help me with this, I am trying for past one or two days but in vain. I am not a hardcore PHP programmer but very much a beginner, any help is really appreciated. Thank you in advance.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Can I assume these roles exist in the DB? When you added them, did you follow the “slug naming convention” (all lower case, no spaces, no punctuation except hyphen) for the role name? The display name can be mixed case with spaces, but not the role “slug”.

    Then when you set or remove roles, use the “slug” name, not the display name.

    BTW, your alert boxes do not work because the browser is not expecting output when the action fires. A good debugging alternative in such cases is error_log(). You can send any text you want with this function, it will appear in your PHP error log. Of course you need immediate access to the log file, which is an issue with some hosts. One more reason serious devs work locally.

    One more thing, very important. Eventually, be sure you validate and sanitize anything in $_POST before it is placed into your DB, otherwise your site is open to SQL injection attacks. For development you can probably get away with not sanitizing for brief periods, but disable such insecure code when you’re done working with it if the site is accessible to the public. Yes, even for form elements like radio buttons without “open” input. Attackers do not need your form to POST something malicious that ends up in $_POST!

Viewing 1 replies (of 1 total)
  • The topic ‘Adding radio choices and updating the role for a new registered user’ is closed to new replies.