Custom Field on Registration

If you want to add a new field on the registration page for Tutor LMS, please follow the snippet below.

In this example, we added a new field for phone numbers. If you follow this method, you can add as many custom fields as you want.

PHP Snippet

This one handles the functionality part. You have to paste this code on your child theme’s functions.php file. You can use a custom plugin if you want as well.

add_filter('tutor_student_registration_required_fields', 'required_phone_no_callback');
if ( ! function_exists('required_phone_no_callback')){
    function required_phone_no_callback($atts){
        $atts['phone_no'] = 'Phone Number field is required';
        return $atts;
    }
}
add_action('user_register', 'add_phone_after_user_register');
add_action('profile_update', 'add_phone_after_user_register');
if ( ! function_exists('add_phone_after_user_register')) {
    function add_phone_after_user_register($user_id){
        if ( ! empty($_POST['phone_no'])) {
            $phone_number = sanitize_text_field($_POST['phone_no']);
            update_user_meta($user_id, 'phone_number', $phone_number);
        }
    }
}

HTML Snippet

This snippet will add the new field in HTML

  <div class="tutor-form-row">

        <div class="tutor-form-col-12">
            <div class="tutor-form-group">
                <label>
                    <?php _e('Phone Number', 'tutor'); ?>
                </label>

                <input type="text" name="phone_no" value="<?php echo tutor_utils()->input_old('phone_no'); ?>" placeholder="<?php _e('Phone Number', 'tutor'); ?>">
            </div>
        </div>

    </div>

The HTML code should go in the registration.php file and you should override it from your theme. The location of the file would be-

yourtheme/tutor/dashboard/registration.php

Here is a gist showing the entire file with the extra code in it.

If you want to add any other custom field in your registration page, then you need to follow the following steps:

  • First, need to add a function in your function.php file
add_filter('tutor_student_registration_required_fields', 'required_nid_no_callback');
if ( ! function_exists('required_nid_no_callback')){
    function required_nid_no_callback($atts){
        $atts['nid'] = 'National Id';
        return $atts;
    }
}
add_action('user_register', 'add_nid_after_user_register');
add_action('profile_update', 'add_nid_after_user_register');
if ( ! function_exists('add_nid_after_user_register')) {
    function add_nid_after_user_register($user_id){
        if ( ! empty($_POST['nid'])) {
            $nid = sanitize_text_field($_POST['nid']);
            update_user_meta($user_id, '_nid', $nid);
        }
    }
}
  • Then, Need to override the registration page and add the following code, to this file path location :

plugins/tutor/templates/dashboard/registration.php

Here you will need to add a field to that registration page

<div class="tutor-form-col-6">
    <div class="tutor-form-group">
        <label>
            <?php _e('NId', 'tutor'); ?>
        </label>

        <input type="text" name="nid" value="<?php echo tutor_utils()->input_old('nid'); ?>" placeholder="<?php _e('NID', 'tutor'); ?>">
    </div>
</div>
  • Now you need to override the my-profile.php file and need to add the following code.

Here is the file path location :

/plugins/tutor/templates/dashboard/my-profile.php

  • Here add a variable first for this at the top of the page
$nid = get_user_meta($uid,'_nid',true);

Then, There add this code

 <div class="tutor-dashboard-profile-item">
            <div class="heading">
                <span><?php _e('NId', 'tutor'); ?></span>
            </div>
            <div class="content">
                
                <p><?php  echo $nid ? $nid : "________"; ?> </p>
            </div>
        </div>
  • Then you will need to override the profile.php file and add the following code, here is the file path location :

/plugins/tutor/templates/dashboard/settings/profile.php

Then, add this:

          <div class="tutor-form-col-6">
                <div class="tutor-form-group">
                    <label>
                        <?php _e('Nid', 'tutor'); ?>
                    </label>
                    <input type="number" min="1" name="nid" value="<?php echo get_user_meta($user->ID,'_nid',true); ?>" placeholder="<?php _e('NId', 'tutor'); ?>">
                </div>
            </div>
  • if you want to add custom fields on registration form then need to edit this file.

/tutor/views/metabox/user-profile-fields.php

then add this:

<tr class="user-description-wrap">
		<th><label for="description"><?php esc_html_e( 'Your Field Name', 'tutor' ); ?></label></th>
		<td>
			<input type="text" name="your_filed_name" id="your_filed_name" value="<?php echo esc_attr( get_user_meta( $user->ID, 'your_filed_name', true ) ); ?>" class="regular-text" />
		</td>
	</tr>

Was this helpful?