Created
December 20, 2017 11:37
-
-
Save mataprasad/89345eef75cf6e4911b8f4008920234a 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
To enable rest end point , wp version must be greater thean 4.7 | |
Now install the plugin disable-json-api.1.4.2 [https://wordpress.org/plugins/disable-json-api/] | |
Now if u activate the plugin and got to plugin option u will see the endpoiunt urls for rest api | |
Now to add your own endpoint, go to your activated theme's functions.php | |
and paste the below demo code | |
---------- | |
/** | |
* | |
* Get The latest post from a category ! | |
* @param array $params Options for the function. | |
* @return string|null Post title for the latest,? * or null if none | |
* | |
*/ | |
function get_latest_post ( $params ){ | |
$array = array( | |
"foo" => "bar" | |
); | |
global $wpdb; | |
$result = $wpdb->get_row("SELECT option_value as target FROM wp100.wp_options where option_id=4"); | |
return $result; | |
$post = get_posts( array( | |
'category' => $category, | |
'posts_per_page' => 1, | |
'offset' => 0 | |
) ); | |
if( empty( $post ) ){ | |
return null; | |
} | |
return $post[0]->post_title; | |
} | |
// Register the rest route here. | |
add_action( 'rest_api_init', function () { | |
register_rest_route( 'mynamespace/v1', 'latest-post',array( | |
'methods' => 'GET', | |
'callback' => 'get_latest_post' | |
) ); | |
} ); | |
---------- | |
this will enable a new endpoint with below rest url | |
/mynamespace/v1/latest-post | |
test the same by hitting below url in the browser | |
http://localhost:100/index.php?rest_route=/mynamespace/v1/latest-post |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment