Registration and Profile custom field

How to add custom field in wordpress registration form and view it in user profile in the backend?

I’ve already google it but just find how to add it in user profile but not in registration process.

Related posts

2 comments

  1. I just went through this whole process myself. There isn’t a hook or anything to make this easy, so instead we have to “hack” it into place in order for it to work. It’s not pretty, but it gets the job done. I’ve been using it for months now with 3 custom fields and it’s been a big time saver to add my custom user info right from the beginning.

    This will add the custom fields to the admin dashboard “Add New User” page. It shouldn’t be too hard to convert this into the public registration page if that’s what you’re trying to do.

    Add all of this code to your theme’s function.php.

    Step 1: Add the fields to the Add User page:

    // Add custom field for wp-admin add user page. 
    // There is no filter or hook, and this is a hack, but it works. 
    function wpse_135622_add_new_user_custom_field_hack(){
        global $pagenow;
        # do this only in page user-new.php
        if($pagenow !== 'user-new.php')
            return;
        # do this only if you can
        if(!current_user_can('manage_options'))
            return false;
        ?>
        <table id="table_custom_field_1" style="display:none;">
        <!-- My Custom Code { -->
            <tr>
                <th><label for="custom_field_1">Custom Field Label</label></th>
                <td><input type="text" name="custom_field_1" id="custom_field_1" /></td>
            </tr>
        <!-- } -->
        </table>
        <script>
        jQuery(function($){
            //Move my HTML code below user's role
            $('#table_custom_field_1 tr').insertBefore($('#role').parentsUntil('tr').parent());
        });
        </script>
        <?php
    }
    add_action('admin_footer_text', 'wpse_135622_add_new_user_custom_field_hack');
    

    Of course, rename every custom_field_1 that you see to something that makes sense to you. Such as pets_name or something.

    Step 2: Now that the field is displaying on the registration form, we need to save it to the database when we click Add New User:

    // Save custom field
    function wpse_135622_save_custom_add_new_user_field_hack($user_id){
        # do this only if you can
        if(!current_user_can('manage_options'))
            return false;
    
        # save my custom field
        update_usermeta($user_id, 'user_custom_field_1', $_POST['custom_field_1']);
    }
    add_action('user_register', 'wpse_135622_save_custom_add_new_user_field_hack');
    

    Again, rename every custom_field_1 to match what you did above.

    Now that it’s saved, this is how you can view it within Admin Dashboard > Users:

    // Add custom fields to user profile page.
    function wpse135622_show_custom_user_fields($profile_fields) {
        $profile_fields['user_custom_field_1'] = 'Custom Field 1';
        return $profile_fields;
    }
    add_filter('user_contactmethods', 'wpse135622_show_custom_user_fields');
    

    Again, match the custom_field_1 to the name you’ve been using, and Custom Field 1 is the label you want to show on the manage user page.

    That should sum it up and get you started. Since it’s in the database now, you can do whatever you want with it – such as display it on the front page if the user is logged in.

    If this answers your question, please click the checkmark to mark it completed.

  2. If by “wordpress registration form” you mean the form at wp-login.php?action=register then there is a hook that can be used to add fields below the “email” line. For example…

    add_action(
      'register_form',
      function() {
        echo 'Here is where the hook fires';
      }
    );
    

    To save the data, hook into user_register as in this example from the Codex:

    add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
    
    function myplugin_registration_save( $user_id ) {
    
        if ( isset( $_POST['first_name'] ) )
            update_user_meta($user_id, 'first_name', $_POST['first_name']);
    
    }
    

    There is very little detail to your question so it is hard to be more specfic.

Comments are closed.