phpwordpresscustom-fieldsadd-filter

Add custom fields to Author Information in WordPress


I'm looking for a way to add custom fields and display them (without plugin).

I've found a great example on the web. The author adds a number of custom fields by adding the following function to the fuctions.php file:

function modify_contact_methods($profile_fields) {

    // Add new fields
    $profile_fields['linkedin'] = 'LinkedIn URL';
    $profile_fields['telephone'] = 'Telephone';        
    return $profile_fields;
}

add_filter('user_contactmethods', 'modify_contact_methods');

I've been able to successfully add such fields to the Contact Information section of my user registration form. I've been trying to add custom fields to other sections, like the Author Information section (where the Bio is), but without success.

I think that I've to change the value user_contactmethods in the add_filter(...) function, but I have not been able to find anything.

I do not even know if this is the correct way to do this, but it worked so far.


Solution

  • As you are new to wordpress, You don't have knowledge about the filter and action. If you go through the filter list , You will find user_contactmethods here.

    As you can see in Author and User Filters, there are only 4 filters for Author and User. And we can use none of them to achieve your desired output.

    But somehow we can do it by adding another field under the About the User something like Author Information.

        add_action( 'show_user_profile', 'extra_user_profile_fields' );
        add_action( 'edit_user_profile', 'extra_user_profile_fields' );
    
        function extra_user_profile_fields( $user ) { ?>
        <h3><?php _e("Author Information", "blank"); ?></h3>
    
        <table class="form-table">
        <tr>
        <th><label for="author"><?php _e("Author Information"); ?></label></th>
        <td>
        <textarea name="author" id="author" rows="5" cols="10" ><?php echo esc_attr( get_the_author_meta( 'author', $user->ID ) ); ?></textarea><br />
        <span class="description"><?php _e("Please enter Author's Information."); ?></span>
        </td>
        </tr>
        </table>
        <?php }
    
        add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
        add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
    
        function save_extra_user_profile_fields( $user_id ) {
    
        if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
    
        update_user_meta( $user_id, 'author', $_POST['author'] );
        }
    

    So in that way you can add as many field as you want.