Guest User

Untitled

a guest
Apr 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. // Extends user profiles with building_id field
  2. function fb_add_custom_user_profile_fields( $user ) {
  3. ?>
  4. <table class="form-table">
  5. <tr>
  6. <th>
  7. <label for="building_id"><?php _e('Building ID', 'your_textdomain'); ?>
  8. </label></th>
  9. <td>
  10. <input type="text" name="building_id" id="building_id" value="<?php echo esc_attr( get_the_author_meta( 'building_id', $user->ID ) ); ?>" class="regular-text" /><br />
  11. <span class="description"><?php _e('Please enter your building_id.', 'your_textdomain'); ?></span>
  12. </td>
  13. </tr>
  14. </table>
  15. <?php }
  16.  
  17. function fb_save_custom_user_profile_fields( $user_id ) {
  18. if ( !current_user_can( 'edit_user', $user_id ) )
  19. return FALSE;
  20. update_usermeta( $user_id, 'building_id', $_POST['building_id'] );
  21. }
  22.  
  23. add_action( 'show_user_profile', 'fb_add_custom_user_profile_fields' );
  24. add_action( 'edit_user_profile', 'fb_add_custom_user_profile_fields' );
  25. add_action( 'personal_options_update', 'fb_save_custom_user_profile_fields' );
  26. add_action( 'edit_user_profile_update', 'fb_save_custom_user_profile_fields' );
  27.  
  28. // Register new field (building _id) and expose it to WP REST API
  29. // See docs: https://developer.wordpress.org/reference/functions/register_rest_field/
  30. add_action( 'rest_api_init', 'create_api_users_meta_field' );
  31.  
  32. function create_api_users_meta_field() {
  33. // Codex format example: register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
  34. register_rest_field( 'user', 'buidling_id', array(
  35. 'get_callback' => 'get_user_meta_for_api',
  36. 'schema' => null,
  37. )
  38. );
  39. }
  40.  
  41. function get_user_meta_for_api( $object ) {
  42. //get the id of the user object array
  43. $user_id = $object['id'];
  44. //return the user meta
  45. return get_user_meta( $user_id );
  46. }
Add Comment
Please, Sign In to add comment