Cory Nickerson
Cory Nickerson

Reputation: 893

Custom User Fields on Registration

I'm working on a custom plugin for a client and I need to add some custom user fields. I've searched through the Codex but couldn't come across the answer.

I basically need to add some new rows to the users table in the MySQL and then add some extra fields during the registration. I'm sure there are other plugins out there that allow you to add custom user fields but I'd like to incorporate it directly into my plugin.

How can I do it?

Upvotes: 2

Views: 561

Answers (1)

brasofilo
brasofilo

Reputation: 26065

I've answered a similar Question at WordPress Answers: Checkboxes in registration form.

You need the action hooks register_form (to inject your input fields) and user_register (to process it). The rest of the code is just sample code to check the results in the pages Profile and User Edit.

// REGISTRATION
add_action( 'register_form', 'signup_fields_wpse_87261' );
add_action( 'user_register', 'handle_signup_wpse_87261', 10, 2 );

// PROFILE
add_action( 'show_user_profile', 'user_field_wpse_87261' );
add_action( 'personal_options_update', 'save_profile_fields_87261' );

// USER EDIT
add_action( 'edit_user_profile', 'user_field_wpse_87261' );
add_action( 'edit_user_profile_update', 'save_profile_fields_87261' );

function signup_fields_wpse_87261() {
?>
    <label>
        <input type="checkbox" name="custom_feature_a" id="custom_feature_a" /> 
        Enable feature A?
    </label>
    <br />
    <label>
        <input type="checkbox" name="custom_feature_b" id="custom_feature_b" /> 
        Enable feature B?
    </label>
    <hr />
<?php
}

function handle_signup_wpse_87261( $user_id, $data = null ) 
{
    $feat_a = isset( $_POST['custom_feature_a'] ) ? $_POST['custom_feature_a'] : false;
    $feat_b = isset( $_POST['custom_feature_b'] ) ? $_POST['custom_feature_b'] : false;
    if ( $feat_a ) 
    {
        add_user_meta( $user_id, 'custom_feature_a', $feat_a );
    }
    if ( $feat_b ) 
    {
        add_user_meta( $user_id, 'custom_feature_b', $feat_b );
    }
}

function user_field_wpse_87261( $user ) 
{
    $feat_a = get_user_meta( $user->ID, 'custom_feature_a', true );
    $feat_b = get_user_meta( $user->ID, 'custom_feature_b', true );
?>
    <h3><?php _e('Custom Fields'); ?></h3>
    <table class="form-table">
        <tr>
            <td>
                <label><?php 
                    printf(
                        '<input type="checkbox" name="custom_feature_a" id="custom_feature_a" %1$s />',
                        checked( $feat_a, 'on', false )
                    );
                    ?>
                    <span class="description"><?php _e('Custom Feature A?'); ?></span>
                    </label>
            </td>
        </tr>
        <tr>
            <td>
                <label><?php 
                    printf(
                        '<input type="checkbox" name="custom_feature_b" id="custom_feature_b" %1$s />',
                        checked( $feat_b, 'on', false )
                    );
                    ?>
                    <span class="description"><?php _e('Custom Feature B?'); ?></span>
                    </label>
            </td>
        </tr>
    </table>
<?php 
}

function save_profile_fields_87261( $user_id ) 
{
    $feat_a = isset( $_POST['custom_feature_a'] ) ? $_POST['custom_feature_a'] : false;
    $feat_b = isset( $_POST['custom_feature_b'] ) ? $_POST['custom_feature_b'] : false;
    update_usermeta( $user_id, 'custom_feature_a', $feat_a );
    update_usermeta( $user_id, 'custom_feature_b', $feat_b );
}

Upvotes: 3

Related Questions