• Resolved seoki

    (@seoki)


    I am trying to use register form and have two radio buttons which will define the role for the user on the site. The radio choice is required. I am able to add the radio buttons, but I am not able to validate the radio selection or save it. I have added the theme-my-login-custom.php in the plugins folder with relevant code. I read thru the documentation and follow it, but in vain. Please, help me. I am new to PHP and wordpress.

    Here is the radio choice added –

    <p>
    <label for="user_role1<?php $template->the_instance(); ?>_m">I am a </label>
    
    <label><input type="radio" name="user_role" id="joinasr1<?php $template->the_instance(); ?>"  class="input"  value="<?php if ( $_POST['user_role'] ) echo 'checked="checked"'; ?>" /> Role1</label>
    
    <label><input type="radio" name="user_role" id="joinasr2<?php $template->the_instance(); ?>"  class="input"  value="<?php if ( $_POST['user_role'] ) echo 'checked="checked"'; ?>" /> Role2</label>
    </p>

    theme-my-login-custom.php has this –
    <?php

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

    I am not sure how to save, this is what I have got so far –

    </strong>function tml_user_register( $user_role ) {
    if ( !empty( $_POST[‘user_role’] ) )
    update_user_meta( $user_role, ‘Role’, $_POST[‘user_role’] );

    }
    add_action( ‘user_register’, ‘tml_user_register’ );`

    Please help me. I am trying to get it right for past two days but in vain :(. Really appreciate and thankful for any help.

    https://www.ads-software.com/plugins/theme-my-login/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter seoki

    (@seoki)

    Here is my update on the code which does not help me either ?? All this is in the theme-my-login-custom.php file.

    In my register page I have two radio choices (Role1/Role2)

    It is adding “None” for role in the user list.

    <?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' );
    
    function register_role( $user_id ) {
    	// Instantiate a user object
    	$user = new WP_User( $user_id );
    echo "<script>alert('".$_POST['user_role']."');</script>";
    
    	// Set your role
    	$user->set_role( $_POST['user_role'] );
    
    	// Destroy user object
    	unset( $user );
    }
    add_action( 'user_register', 'register_role' );
    
    ?>

    The echo wont even trigger to get an alert box to show the $_POST[‘user_role’]
    But the error module catches if I don’t select a role in my register page.

    This is the second approach that I am taking, which is not working either –
    Even in this approach error module catches if I don’t select a role in my register page.But echo alert wont trigger for me to debug which tells me its not even coming to this function

    function tml_user_register( $user_id ) {
    $user = new WP_User( $user_id );
    
        if (isset($_POST['user_role'] ) ) && $_POST['user_role'] = 'Role1' ){
        echo "<script>alert('" . $_POST['user_role'] . "');</script>";
        $user->set_role( $_POST['user_role'] );
    	unset( $user );
       }else{
        echo "<script>alert('" . $_POST['user_role'] . "');</script>";
        $user->set_role( 'Role2' );
    	unset( $user );
        }

    //add_action( ‘user_register’, ‘tml_user_register’ );

    Please somebody help me on this.

    Plugin Author Jeff Farthing

    (@jfarthing84)

    Try setting a higher priority on your user_register callback.

    add_action( 'user_register', 'register_role', 5 );
    Thread Starter seoki

    (@seoki)

    Hi Jeff, thank you so much. That worked great! Thank you again ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Radio button implementation’ is closed to new replies.