Created
October 21, 2022 12:08
-
-
Save wbaxterh/b6a14bf8de6819efb8a907ce1495b55d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Add a timezone field to user profile - Wes Huber 10/2022 | |
add_action( 'rest_api_init', 'adding_user_meta_rest' ); | |
function adding_user_meta_rest() { | |
register_rest_field( 'user', | |
'timezone', | |
array( | |
'get_callback' => 'user_meta_callback', | |
'update_callback' => 'timezone_update', | |
'schema' => null, | |
'show_in_rest' => true, | |
'auth_callback' => 'permission_check', | |
) | |
); | |
} | |
/** | |
* Check permissions | |
* | |
* @param WP_Rest_Request $request | |
* @return bool | |
*/ | |
function permission_check( $request ) { | |
return true; | |
} | |
function timezone_update($value){ | |
$user_id = get_current_user_id(); | |
$field_name = 'timezone'; | |
return update_user_meta( $user_id, $field_name, $value ); | |
} | |
function user_meta_callback( $user, $field_name, $request) { | |
$user_id = get_current_user_id(); | |
$field_name = 'timezone'; | |
return get_user_meta( $user_id, $field_name, true ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment