How To Add Custom Form Fields To The User Profile Page?

6 Aug

How To Add Custom Form Fields To The User Profile Page?

Context: You want to add, update, delete, extra fields on the User profile Page in WordPress backend.

Solution: There are lits of action hoooks like, ‘show_user_profile’, ‘edit_user_profile’, ‘personal_options_update’ and ‘edit_user_profile_update’ that can be used to achieve this task.

add_action( 'show_user_profile', 'show_extra_user_profile_fields' );
add_action( 'edit_user_profile', 'show_extra_user_profile_fields' );
function show_extra_user_profile_fields( $user ) {

  echo '<h3>Payment Method</h3>';
  echo '<table class="form-table">';
   echo ' <tr>';
      echo '<th><label for="paypal">Paypal ID</label></th>';
      echo '<td>';
        echo '<input type="text" name="paypal" id="paypal" class="regular-text" 
            value="'.esc_attr( get_the_author_meta( 'paypal', $user->ID ) ).'" />';
        echo '<span class="description">Please enter your paypal Id Here</span>';
    echo '</td>';
    echo '</tr>';
  echo '</table>';

}

add_action( 'personal_options_update', 'paypal_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'paypal_save_extra_user_profile_fields' );
function paypal_save_extra_user_profile_fields( $user_id ) {
  $saved = false;
  if ( current_user_can( 'edit_user', $user_id ) ) {
    update_user_meta( $user_id, 'paypal', $_POST['paypal'] );
    $saved = true;
  }
  return true;
}

Bydr.code.skm

I am backend developer with passion in Web Application development using latest technologies like Laravel, PHP7, React, ECMAScript 6 and WordPress. I prefer spending time analyzing big data with Apache Spark. Apart from that, I do photography.