Rest Api WpWordpress HooksWordpress Tricks

How to get all users in wordpress with rest api hook?

get all users in wordpress with rest api hook

wordpress
1.21KViews

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

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

2 Comments

  • Hi I want to access wordpress plugin list in my other website which is non wordpress.

    How to do ?
    Example:

    Site 1(WordPress) —–> plugin names (Akismet,woocommerce) ——–> access the names only in my site 2 (Non wordpress).

    Just as a information I want to access it.

    • Hello Khaled, Thank you for your comment.
      You can use your WordPress website information in your non wordpress website with api’s.
      With wordpress rest api’s, you can any informations and used that one into your non-wordpress site.

      Thank you

Leave a Reply

Positive SSL