Add Custom Fields In Accounts Edit Page For WordPress

Table of Contents

Enhance WordPress user profiles by adding a custom birthday field. Two functions work together: one displays the input field on the profile edit page, while the other saves the entered data. This simple code snippet allows for easy expansion of user information within the WordPress admin area.

Original Posts

Add Custom Fields In Accounts Edit Page For WordPress


add_action( 'show_user_profile', 'add_birthday_meta' );
add_action( 'edit_user_profile', 'add_birthday_meta' );
function add_birthday_meta( $user ) {
  $birthday = get_user_meta ($user->ID , 'birthday', true); ?>
  <table class="form-table">
    <tr>
      <th><label for="birthday">生日</label></th>
        <td><input type="text" id="birthday" name="birthday" value="<?php echo $birthday ; ?>"></td>
      </tr>
  </table>
  <?php
}
add_action( 'personal_options_update', 'save_user_birthday' );
add_action( 'edit_user_profile_update', 'save_user_birthday' );
function save_user_birthday( $user_id ) {
  if ( ! current_user_can( 'edit_user', $user_id ) ) {
    return false;
  }
  update_user_meta( $user_id, 'birthday', $_POST['birthday'] );
}

Related Posts

The Complete WordPress Customization Tutorial – Just Copy And Paste And Work

🚀 Unlock Ads-Free Experience At $5/year

✓ 14 days free trial ✓ Cancel anytime

Leave a Comment

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