Categories

Friday, November 22, 2024
#919814419350 therichposts@gmail.com
Rest Api WpWordpress HooksWordpress Tricks

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

How to add custom meta title and meta description in Wordpress?

get all users in wordpress with rest api hook

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.