Clean Login custom user meta fields WP snippet

I just made extra fields in my user registration and made it appear in the clean login [edit-profile] shortcode. Here’s a wp snippet that you can modify to do the same. I registered two custom user meta fields via formidable forms, but I can only view it if I display the form in the frontend. Formidable Forms lack the confirm email functionality when you change the email address in the web form, so I used clean login to achieve a frontend edit profile with e-mail confirmation.

Below is an example snippet to add more fields in your clean login edit profile page.

<?php
// Add custom user meta fields to the Clean Login edit form
add_action( 'clean_login_edit_profile_fields', function( $user_id ) {
    $contact   = get_user_meta( $user_id, 'contact', true );
    $territory = get_user_meta( $user_id, 'territory', true );
    ?>
    <p>
        <label for="contact"><?php _e( 'Contact', 'clean-login' ); ?></label><br>
        <input type="text" name="contact" id="contact" value="<?php echo esc_attr( $contact ); ?>" />
    </p>
    <p>
        <label for="territory"><?php _e( 'Territory', 'clean-login' ); ?></label><br>
        <input type="text" name="territory" id="territory" value="<?php echo esc_attr( $territory ); ?>" />
    </p>
    <?php
});

// Save the custom fields when user updates profile
add_action( 'clean_login_save_profile_fields', function( $user_id ) {
    if ( isset( $_POST['contact'] ) ) {
        update_user_meta( $user_id, 'contact', sanitize_text_field( $_POST['contact'] ) );
    }
    if ( isset( $_POST['territory'] ) ) {
        update_user_meta( $user_id, 'territory', sanitize_text_field( $_POST['territory'] ) );
    }
});

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.