WordPress.org

Ready to get started?Download WordPress

Forums

Theme My Login
[resolved] Extra registration fields... checkbox and textarea. (8 posts)

  1. Epaxido
    Member
    Posted 1 year ago #

    Hi Jeff, very nice plugin.

    I've added a checkbox and a textarea in registration page and both are showed in the profile page but don't returns user's data.

    This are the settings I used..

    on register-form.php

    <p>
       <label for="user_note<?php $template->the_instance(); ?>"><?php _e( 'Note', 'theme-my-login' ) ?></label>
       <textarea rows="4" cols="45" name="user_note" id="user_note<?php $template->the_instance(); ?>" class="input" value="<?php $template->the_posted_value( 'user_note' ); ?>"></textarea>
    </p>
    <p>
       <input type="checkbox" name="user_ckb" id="user_ckb<?php $template->the_instance(); ?>" value="<?php $template->the_posted_value( 'user_ckb' ); ?>"  />
       <label for="user_ckb<?php $template->the_instance(); ?>"><?php _e("Ckb"); ?></label>
    </p>

    on profile-form.php

    <tr>
    <th><label for="user_note"><?php _e( 'Note', 'theme-my-login' ) ?></label></th>
    <td><textarea rows="4" cols="45" name="user_note" id="user_note" class="input" value="<?php echo esc_attr( $profileuser->user_note ) ?>"></textarea></td>
    </tr>
    <tr>
    <th><label for="user_ckb"><?php _e( 'Ckb', 'theme-my-login' ) ?></label></th>
    <td><input type="checkbox" name="user_ckb" id="user_ckb" value="<?php echo esc_attr( $profileuser->user_ckb) ?>" /></td>
    </tr>

    on theme-my-login-custom.php

    function tml_user_register( $user_id ) {
       if ( !empty( $_POST['user_note'] ) )
       update_user_meta( $user_id, 'user_note', $_POST['user_note'] );
       if ( !isset( $_POST['user_ckb'] ) )
       update_user_meta( $user_id, 'user_ckb', $_POST['user_ckb'] );
    }
    add_action( 'user_register', 'tml_user_register' );
    function tml_edit_user_profile( $profileuser ) {
       <tr>
          <th><label for="user_note"><?php _e( 'Note', 'theme-my-login' ) ?></label></th>
          <td><textarea rows="4" cols="45" name="user_note" id="user_note" value="<?php echo esc_attr( $profileuser->user_note ) ?>"></textarea></td>
       </tr>
       <tr>
          <th><label for="user_ckb"><?php _e( 'Ckb', 'theme-my-login' ) ?></label></th>
          <td><input type="checkbox" name="user_ckb" id="user_ckb" value="<?php echo esc_attr( $profileuser->user_ckb) ?>" /></td>
       </tr>
    }
    add_action( 'edit_user_profile', 'tml_edit_user_profile' );

    Does anyone know where I'm wrong? thanks for the help

    http://wordpress.org/extend/plugins/theme-my-login/

  2. Jeff Farthing
    Member
    Plugin Author

    Posted 1 year ago #

    What's not working?

  3. Epaxido
    Member
    Posted 1 year ago #

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

  4. Epaxido
    Member
    Posted 1 year ago #

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

  5. Epaxido
    Member
    Posted 1 year ago #

    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

  6. schwarzgrau
    Member
    Posted 1 year ago #

    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' );
  7. selnomeria
    Member
    Posted 12 months ago #

    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);
  8. Bshah
    Member
    Posted 9 months ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic