Advertisement
valuser

required-profile-field

Sep 24th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.29 KB | None | 0 0
  1. function extra_user_profile_fields( $user ) { ?>
  2. <table class="form-table">
  3. <tr>
  4. <th><label for="location"><?php _e("Location:",'valentino'); ?> <span class="description"><?php _e('(required)'); ?></span></label></th>
  5. <td>
  6. <input type="text" name="location" id="location" value="<?php echo esc_attr( get_the_author_meta( 'location', $user->ID ) ); ?>" class="regular-text"  /><br />
  7. <span class="description"><?php _e("Please enter your location.",'valentino'); ?>   </span>
  8. </td>
  9. </tr>
  10. <?php }
  11.  
  12. add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
  13. add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
  14.  
  15.  
  16. add_filter('user_profile_update_errors', 'valentino_check_fields', 10, 3);
  17. function valentino_check_fields($errors, $update, $user) {
  18.  
  19.   // Use the $_POST variable to check required fields
  20.  
  21.   if( empty($_POST['location']) )
  22.     // add an error message to the WP_Errors object
  23.     $errors->add( 'location_required',__('Location is required, please add one before saving.') );
  24.  
  25.   // Add as many checks as you have required fields here
  26.  
  27.   if( empty( $errors->errors ) ){
  28.  
  29.     // Save your custom fields here if no errors are found
  30.     // Just skip this if you don't need to do extra work.
  31.     // Fields will save if no errors are found
  32.   }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement