Quantcast
Channel: WordPress.org Forums » All Posts
Viewing all articles
Browse latest Browse all 119588

Epaxido on "[Plugin: Theme My Login] Extra registration fields... checkbox and textarea."

$
0
0

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


Viewing all articles
Browse latest Browse all 119588

Trending Articles