• Hi all.

    I spent some time figuring this out and though someone my use it.

    This code will allow you to add extra fields to Theme My Login and display them in the user front end as well as admin back end.

    We are going to create two additional fields:

    1. Job Title
    2. Tel number

    1 Create a file called theme-my-login-custom.php in wp-content/plugins folder and paste the following code :

    <?php
    
    function tml_registration_errors( $errors ) { //This will add form validation to your extra fields.
    if ( empty( $_POST['job_title'] ) )
    $errors->add( 'empty_job_title', '<strong>ERROR</strong>: Job title is required' );
    
    if ( empty( $_POST['tel_number'] ) )
    $errors->add( 'empty_tel_number', '<strong>ERROR</strong>: Tel number Required' );
    return $errors;
    }
    
    add_filter( 'registration_errors', 'tml_registration_errors' );
    
    function tml_user_register( $user_id ) { //register the extra fields
    if ( !empty( $_POST['job_title'] ) )
    update_user_meta( $user_id, 'job_title', $_POST['job_title'] );
    }
    
    if ( !empty( $_POST['tel_number'] ) )
    update_user_meta( $user_id, 'tel_number', $_POST['job_title'] );
    }
    
    add_action( 'user_register', 'tml_user_register' );

    2. On TML theme and profile page add the following code (profile-form.php register-form.php) Make sure you copied theme files from wp-content/plugin/theme-my-login/temapltes to wp-content/themes/your-theme

    <tr>
    <th>
    <label for="job_title"><?php _e( 'Job Title' ); ?></label>
    </th>
    
    <td>
    <input type="text" name="job_title" id="job_title" value="<?php echo esc_attr( $profileuser->job_title ); ?>" class="regular-text" />
    </td>
    </tr>
    
    <tr>
    <th>
    <label for="tel_number"><?php _e( 'Tel Number' ); ?></label>
    </th>
    
    <td>
    <input type="text" name="tel_number" id="tel_number" value="<?php echo esc_attr( $profileuser->tel_number); ?>" class="regular-text" />
    </td>
    </tr>

    3 Last thing – register your new filed in admin. Past this code to your wp-content/themes/your-theme/function.php

    <h3>Extra profile information</h3>
    
    <table class="form-table">
    <tr>
    <th>
    <label for="job_title">job Title</label>
    </th>
    <td>
    <input type="text" name="job_title" id="job_title" value="<?php echo esc_attr( get_the_author_meta( 'job_title', $user->ID ) ); ?>" class="regular-text" /><br />
    </td>
    </tr>
    
    <tr>
    <th>
    <label for="tel_number">Tel Number</label>
    </th>
    <td>
    <input type="text" name="tel_number" id="tel_number" value="<?php echo esc_attr( get_the_author_meta( 'tel_number', $user->ID ) ); ?>" class="regular-text" /><br />
    <span class="description">This is user tel number.</span>
    </td>
    </tr>
    </table>

    Hope this will help someone

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi,

    thanks for your tutorial, expanding on the one given by the developer at http://www.jfarthing.com/development/theme-my-login/adding-extra-registration-fields/

    Quick question: will this method survive a theme update?

    Step 3 does not work for me. When I enter this part of the code, the user edit panel does not appear for admin. Once I comment out from <h3> to the first </tr> the user edit panel reappears. I copy and pasted the text in your example.

    Ok, I found the culprit for item #3 above. I had to put in a ?> prior to the <h3> tag. However, now those fields appear at the top of the user edit admin panel, outside the theme. Also, they aren’t populated with the information entered for registration.

    Thread Starter dompl

    (@dompl)

    Sorry guys. Here is the one that will work. And yes @attenkind, It will work after upgrade.

    <?php
    function tml_registration_errors( $errors ) {
    	if ( empty( $_POST['first_name'] ) )
    		$errors->add( 'empty_first_name', '<strong>ERROR</strong>: Please enter your first name.' );
    	if ( empty( $_POST['last_name'] ) )
    		$errors->add( 'empty_last_name', '<strong>ERROR</strong>: Please enter your last name.' );
    	if ( empty( $_POST['job_title'] ) )
    		$errors->add( 'empty_job_title', '<strong>ERROR</strong>: Job title is required' );
    	return $errors;
    }
    add_filter( 'registration_errors', 'tml_registration_errors' );
    
    function tml_user_register( $user_id ) {
    	if ( !empty( $_POST['first_name'] ) )
    		update_user_meta( $user_id, 'first_name', $_POST['first_name'] );
    	if ( !empty( $_POST['last_name'] ) )
    		update_user_meta( $user_id, 'last_name', $_POST['last_name'] );
    	if ( !empty( $_POST['job_title'] ) )
    		update_user_meta( $user_id, 'job_title', $_POST['job_title'] );
    	if ( !empty( $_POST['company'] ) )
    		update_user_meta( $user_id, 'company', $_POST['company'] );
    	if ( !empty( $_POST['country'] ) )
    		update_user_meta( $user_id, 'country', $_POST['country'] );
    }
    add_action('show_user_profile', 'tml_user_register');
    add_action('edit_user_profile', 'tml_user_register');
    
    function my_save_extra_profile_fields( $user_id ) {
    
        if ( !current_user_can( 'edit_user', $user_id ) )
        return false;
    
        if(!empty( $_POST['company'])){
            update_usermeta( $user_id, 'company', $_POST['company'] );
        }
    
        if(!empty( $_POST['job_title'])){
            update_usermeta( $user_id, 'job_title', $_POST['job_title'] );
        }
    
        if(!empty( $_POST['country'])){
            update_usermeta( $user_id, 'country', $_POST['country'] );
        }
    }
    add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    ?>

    WordPress Hook

    function my_show_extra_profile_fields( $user ) { ?>
    <table class="form-table">
    <tr>
    <th><label for="job_title">Job Title</label></th>
    <td>
    <input type="text" name="job_title" id="job_title" value="<?php echo esc_attr( get_the_author_meta( 'job_title', $user->ID ) ); ?>" class="regular-text" /><br />
    </td>
    </tr>
    <tr>
    <th><label for="company">Company</label></th>
    
    <td>
    <input type="text" name="company" id="company" value="<?php echo esc_attr( get_the_author_meta( 'company', $user->ID ) ); ?>" class="regular-text" /><br />
    </td>
    </tr>
    <tr>
    <th><label for="country"><?php _e( 'Country' ); ?></label></th>
    <td><?php $current = get_the_author_meta( 'country', $user->ID ); ?>
    <select name="country" id="country" class="regular-country">
    <?php
    $current = get_the_author_meta( 'country', $user->ID );
    $countries = array('United Kingdom','Afghanistan''Zimbabwe','Aland Islands');
    foreach ($countries as $key => $value) { ?>
    <option <?php echo($value==$current) ? 'selected="selected"' : ''; ?> value="<?php echo $value; ?>"><?php echo $value; ?></option>';
    <?php } ?>
    </select>
    </td>
    </tr>
    </table>
    <?php   }   ?>

    @rattenkind – I just updated my theme and sure enough it killed all of my files. I suggest you perform a backup of any of your modified files just in case.

    Tom

    Hey dompl, can you edit these fields and get them to save from the User Edit admin panel? I see the fields at the bottom of the page, but if I try to modify them at all and save the page it does not update the fields or the database.

    Tom

    ok guys, it seems there’s no easy, workable (and reliable) way to do custom fields with TML.

    So now I’m testing the “Users Ultra” plugin (the free/lite version) – which does exactly what I need:
    -show a login form within my theme (also with social login options)
    -easily editable custom user fields
    -a front-end for users to edit their profile fields
    -it easily works with an existing user base (you just need to press synchronize once)

    I’m still in the process of testing it out, but it looks very good. Maybe others will also find this to be a good alternative to hacking TML.

    I’m posting this because the “Users Ultra” description is a bit vague and therefore it’s not straightforward to find when you are just looking for a good login with custom fields plugin…

    May I know how I enable the my_show_extra_profile_fields()? I can’t see any add_action() for this function.

    I added a extra field “mobile”, user can see it from his/her user-profile/ but as a administrator, I cannot see this field from the dashboard, how can I add this to dashboard view and manage it?

    Best regards,

    Kelvin.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Register extra/additional fields for Theme My Login including admin’ is closed to new replies.