Last active
January 27, 2016 19:42
-
-
Save martijn94/e9f9bd138e1afe4d9cab to your computer and use it in GitHub Desktop.
WP api callback function
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
<?php | |
function wpc_somename_search_callback( $request_data ) { | |
$parameters = $request_data->get_params(); | |
if( !isset( $parameters['keyword'] ) || empty($parameters['keyword']) ) | |
return array( 'error' => 'no_parameter_given' ); | |
$keyword = $parameters['keyword']; | |
if( strlen( $keyword ) <= 3 ) | |
return array( 'error' => 'keyword_not_long_enough' ); | |
$output = wp_remote_get('https://thirdpartyapi.com/suggest/' . $keyword); | |
if( $response = json_decode($output['body'], true) ) { | |
if( isset( $response['error'] ) ) | |
return array( 'error' => 'error_when_retrieving_data' ); | |
return $response; | |
} else { | |
return array( 'error' => 'something_went_wrong_when_retrieving_data' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment