Skip to content

Instantly share code, notes, and snippets.

@martijn94
Last active January 27, 2016 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martijn94/e9f9bd138e1afe4d9cab to your computer and use it in GitHub Desktop.
Save martijn94/e9f9bd138e1afe4d9cab to your computer and use it in GitHub Desktop.
WP api callback function
<?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