Created
August 11, 2016 15:48
-
-
Save jprieton/b22197da5cc5d31f0a18701a238d603e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
add_action( 'rest_api_init', function () { | |
register_rest_route( 'me/favorite', 'add', array( | |
'methods' => 'GET', | |
'callback' => 'rest_test', | |
) ); | |
} ); | |
function wp_send_rest_success( $response = array() ) { | |
$defaults = array( | |
'code' => '', | |
'message' => '', | |
'success' => true, | |
'data' => array( 'status' => '200' ), | |
); | |
$response = wp_parse_args( $response, $defaults ); | |
wp_send_json( $response ); | |
} | |
function wp_send_rest_error( $response = array() ) { | |
$defaults = array( | |
'code' => '', | |
'message' => '', | |
'success' => false, | |
'data' => array( 'status' => '200' ), | |
); | |
$response = wp_parse_args( $response, $defaults ); | |
wp_send_json( $response ); | |
} | |
function rest_test() { | |
print_r(func_get_args()); die; | |
if ( !is_user_logged_in() ) { | |
$response = array( | |
'code' => 'unauthorized', | |
'message' => __( 'You are not authorized to perform this action', 'smg' ), | |
'data' => array( 'status' => '403' ) | |
); | |
wp_send_rest_error( $response ); | |
} | |
wp_send_rest_error( array( 'code' => 'ok' ) ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment