Custom Fields for User Profiles?

Hello, does this plugin work for adding custom fields to user profiles? If not, do you know how to do this? Either with a plugin or with custom theme modifications.

  • Ash

    Hello @Alfredo

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    I am afraid, this plugin can't create custom field for user profile. You can try the following code, where I have shown an example to add custom field for facebook URL, you can change it according to your need:

    <?php
    add_action( 'show_user_profile', 'extra_fields' );
    add_action( 'edit_user_profile', 'extra_fields' );
    add_action( 'personal_options_update', 'save_fields' );
    add_action( 'edit_user_profile_update', 'save_fields' );
    
    function extra_fields($user) {
    ?>
    <h3><?php _e("Extra profile information", "DOMAIN"); /*DOMAIN = Lang domain for l10n (optional)*/ ?></h3>
    <table class="form-table">
    <tbody>
    <tr>
    <th><?php _e("Extra profile information", "DOMAIN"); /*DOMAIN = Lang domain for l10n (optional)*/ ?></th>
    <td><input class="regular-text" id="facebook" type="text" name="facebook" value="<?php echo esc_attr( get_the_author_meta( 'facebook', $user->ID ) ); ?>" /></td>
    </tr>
    </tbody>
    </table>
    <?php }
    
    function save_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) ) { return false; } //Checking if the current user has ability to edit the user profile information
    update_user_meta( $user_id, 'facebook', $_POST['facebook'] );
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.