Hello, welcome to therichpost.com. In this post, I will tell you How to get all users in wordpress with rest api hook? WordPress is the best cms. WordPress hooks(add_action, add_filter) give us the power to edit or change the code without interruption into the files and this is the best thing about wordpress. Now I am going to tell you how the hooks work.
rest_api_init function will user to make api.
Today I am introducing WordPress Rest Api. Rest Api is very good feature or hook in wordpress. There are some defaults Rest Api in WordPress and we can also create our own Rest Api in WordPress.
Here is the working image to get users names in WordPress with Rest Api:

I am working with WordPress from long 4 years but today first time I created WordPress Rest Api.
Finally here is the working WordPress Rest Api and you need to add this code into your theme’s functions.php file:
add_action( ‘rest_api_init’, function () {
register_rest_route( ‘wp/v2’, ‘/getusers/’, array(
‘methods’ => ‘GET’,
‘callback’ => ‘get_users_names’
) );
} );function get_users_names()
{
global $wpdb;
$myArr = array();
$wp_user_search = $wpdb->get_results(“SELECT ID, display_name FROM $wpdb->users ORDER BY ID”);foreach ( $wp_user_search as $userid ) {
$myArr[] = stripslashes($userid->display_name);
}
$myJSON = json_encode($myArr);
echo $myJSON;
die();
}
I personally liked it very much WordPress Rest Api. We will use this in Angularjs 2 in future.
There are so many hooks in wordpress and i will let you know all. Please do comment if you any query related to this post. Thank you. Therichpost.com

Leave a Reply
You must be logged in to post a comment.