Support » Plugin: Theme My Login » Extra registration fields… checkbox and textarea.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Jeff Farthing

    (@jfarthing84)

    What’s not working?

    Epaxido

    (@epaxido)

    user selection for checkboxes and text for textarea are not showed in the profile page.

    Epaxido

    (@epaxido)

    can you check the code I’ve posted.. I think is correct, I just need a confirmation, to exclude that code.

    For who need to add checkboxes in the registration form.. here is the code to put in functions (obviously works also without plugin):

    <?php
    add_action( 'register_form', 'signup_fields_wpse_87261' );
    add_action( 'user_register', 'handle_signup_wpse_87261', 10, 2 );
    add_action( 'show_user_profile', 'user_field_wpse_87261' );
    add_action( 'edit_user_profile', 'user_field_wpse_87261' );
    add_action( 'personal_options_update', 'save_profile_fields_87261' );
    add_action( 'edit_user_profile_update', 'save_profile_fields_87261' );
    
    function signup_fields_wpse_87261() {
    ?>
        <input type="checkbox" name="custom_feature_a" id="custom_feature_a" value="enabled" /> Enable feature A?
    
        <input type="checkbox" name="custom_feature_b" id="custom_feature_b" value="enabled" /> Enable feature B?
        <hr />
    <?php
    }
    
    function handle_signup_wpse_87261( $user_id, $data = null )
    {
        $custom_feature_a = $_POST['custom_feature_a'];
        $custom_feature_b = $_POST['custom_feature_b'];
        if ( isset( $custom_feature_a ) )
        {
            add_user_meta( $user_id, 'custom_feature_a', $custom_feature_b );
        }
        if ( isset( $custom_feature_b ) )
        {
            add_user_meta( $user_id, 'custom_feature_b', $custom_feature_b );
        }
    }
    
    function user_field_wpse_87261( $user )
    {
        $custom_feature_a = get_user_meta( $user->ID, 'custom_feature_a', true );
        $custom_feature_b = get_user_meta( $user->ID, 'custom_feature_b', true );
    ?>
        <h3><?php _e('Custom Fields'); ?></h3>
        <table class="form-table">
            <tr>
                <td><span class="description"><?php _e('Custom Feature A?'); ?></span>
                    <label><?php
                        printf(
                            '<input type="checkbox" name="custom_feature_a" id="custom_feature_a" value="enabled" %1$s />',
                            checked( $custom_feature_a, 'enabled', false )
                        );
                        ?></label>
                </td>
            </tr>
            <tr>
                <td><span class="description"><?php _e('Custom Feature B?'); ?></span>
                    <label><?php
                        printf(
                            '<input type="checkbox" name="custom_feature_b" id="custom_feature_b" value="enabled" %1$s />',
                            checked( $custom_feature_b, 'enabled', false )
                        );
                        ?></label>
                </td>
            </tr>
        </table>
    <?php
    }
    
    function save_profile_fields_87261( $user_id )
    {
        update_usermeta( $user_id, 'custom_feature_a', $_POST['custom_feature_a'] );
        update_usermeta( $user_id, 'custom_feature_b', $_POST['custom_feature_b'] );
    }
    ?>

    [Please post code or markup snippets between backticks or use the code button. Or better still – use a pastebin. Your posted code may now have been permanently damaged/corrupted by the forum’s parser.]

    thanks to brasofilo

    Oh then I’ll add my select-box code too.

    This one goes in your template-file like register-form.php

    <p>
    	<select name="gender" id="gender" class="input">
    		<option value="" <?php $template->the_posted_value( 'gender' ); ?> >Please choose your gender</option>
        	<option value="male" <?php $template->the_posted_value( 'gender' ); ?> >Male</option>
      		<option value="female" <?php $template->the_posted_value( 'gender' ); ?> >Female</option>
      		<option value="zac-efron" <?php $template->the_posted_value( 'gender' ); ?> >Between</option>
    	</select>
    </p>

    This part goes in your theme-my-login-custom.php to give out an error, if nothing is selected

    function gender_registration_errors( $errors ) {
    	if ( empty( $_POST['gender'] ) )
    		$errors->add( 'empty_gender', '<strong>ERROR</strong>: Please choose your Gender.' );
    	return $errors;
    }
    add_filter( 'registration_errors', 'gender_registration_errors' );

    This one goes in your theme-my-login-custom.php too to add the select-field to the user-profile and update them.

    function gender_select( $user ) {
        $sex = get_the_author_meta( 'gender', $user->ID);
    ?>
    
        <table class="form-table">
            <tr>
                <th>
                	<label for="gender">gender
                	</label>
                </th>
                <td>
                	<select name="gender" id="gender" class="input">
              			<option <?php if ($sex == 'male' ) { ?>selected<?php }?> value="male">Male</option>
      					<option <?php if ($sex == 'female' ) { ?>selected<?php }?> value="female">Female</option>
      					<option <?php if ($sex == 'zac-efron' ) { ?>selected<?php }?> value="zac-efron">Between</option>
    				</select>
                </td>
            </tr>
        </table>
    <?php
    }
    function my_save_custom_user_profile_fields( $user_id ) {
        if ( !current_user_can( 'edit_user', $user_id ) )
            return FALSE;
    
        update_usermeta( $user_id, 'gender', $_POST['gender'] );
    
        }
    
    add_action( 'show_user_profile', 'gender_select' );
    add_action( 'edit_user_profile', 'gender_select' );
    add_action( 'personal_options_update', 'my_save_custom_user_profile_fields' );
    add_action( 'edit_user_profile_update', 'my_save_custom_user_profile_fields' );

    Here is another code, which helps you to validate, if a checkbox was checked, and ONLY after it, the user registration data will be saved into database:

    function signup_checboxxx() {
    ?>  Hi user, click this - <input type="checkbox" name="field_blabla" value="enabled" />  <br /><br />
    <?php }
    
    add_action( 'register_form', 'signup_checboxxx' );
    
    function myplugin_check_fields($errors, $sanitized_user_login, $user_email) {
    	if ( !isset($_POST['field_blabla']) )
    	{
        $errors->add( 'demo_error', __('<strong>ERROR</strong>: THis is a demo error. Registraition failed.','mydomain') );
        return $errors;
    	}
    }
    
    add_filter('registration_errors', 'myplugin_check_fields', 10, 3);

    Hi,

    I have created my code as per below. I tried to put this hooks call in many files however did not find the right way to do it. Please suggest in 1. which files I should put following call to get it on registration form
    2. I want to save checkbox data in User Meta fields for that do I need to add user id? if yes how? cause the user is still not created.
    3. Should this functions access modifier be PUBLIC?

    Where to put following code in which files?

    add_action( 'register_form',       array( &$this, 'X_membership_registration'   ) );
    
    add_action( 'user_register', 'handle_X_member_registration', 10, 3);
    
    public function X_membership_registration(){
    	?>
    	<div class="grid-6-12">
    		<label>
    			<input type="checkbox" name="dbem_X_member_f" value="1" id="dbem_X_member_f" />
    			I am <strong>Member</strong> of Indian Cartilage Society.
    		</label>
    		</div>
    		<div class="grid-6-12">
    		<label>
    			<input type="checkbox" name="dbem_X_2013_delegate" value="1" id="dbem_X_2013_delegate" />
    			I am <strong>Delegate</strong> of Indian Cartilage Society conference.
    		</label>
    		</div>
    		<div class="grid-6-12">
    		<label>
    			<input type="checkbox" name="dbem_X_sponsor" value="1" id="dbem_X_sponsor" />
    			I am <strong>Sponsor</strong>.
    		</label>
    		</div>
    	<?php
    	}
    
    public function handle_X_member_registration( $user_id, $data = null )
    	{
    		/*echo $_POST['dbem_X_member_f'];
    		echo $_POST['dbem_X_2013_delegate'];
    		echo $_POST['dbem_X_sponsor'];
    		exit;*/
    		$isXMember = isset( $_POST['dbem_X_member_f'] ) ? $_POST['dbem_X_member_f'] : false;
    		$isXDelegate = isset( $_POST['dbem_X_2013_delegate'] ) ? $_POST['dbem_X_2013_delegate'] : false;
    		$isXSponsor = isset( $_POST['dbem_X_sponsor'] ) ? $_POST['dbem_X_sponsor'] : false;
    		if ( $isXMember )
    		{
    			add_user_meta( $user_id, 'dbem_X_member_f', $isXMember );
    		}
    		if ( $isXDelegate )
    		{
    			add_user_meta( $user_id, 'dbem_X_2013_delegate', $isXDelegate );
    		}
    		if ( $isXSponsor )
    		{
    			add_user_meta( $user_id, 'dbem_X_sponsor', $isXSponsor );
    		}
    	}

    Thanks for your time.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Extra registration fields… checkbox and textarea.’ is closed to new replies.