Search code examples
phpwordpresscustom-wordpress-pageswordpress-rest-api

wordpress Get user count from other site


I have a question about the possibility of a user count from another site. The company I work at has an account for every employee on our main-site WordPress, which we display on the our-people page, making it dynamic.

The company I work at also has a website for a specific product with a employee-counter, only it is static.

Is it possible to get the user count from our main website and display it on our product-website, thus making it dynamic?

EDIT: Think it might be possible with a AJAX request, but don't know how...

@misorude The other site is also WordPress. With static I only meant the employee number is static, and needs to be dynamic. The sites are hosted at the same provider, but on different packets.

EDIT 2:
The new problem is basically this: How do I let only the client side interact with those API calls? I don't want those API calls to be public and be called by simply entering the link on browser.

edit 3:

function getUserAmount() {
    $users = get_users();
   
    if ( empty( $users ) ) {
      return 'There aren\'t any users to display.';
    }
    return count($users);
}

/* Preparing to serve an API request */
add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v2', '/wp/colleagues', 
        array(
            'methods' => 'GET',
            'callback' => 'getUserAmount',
        )
    );
} );

How can I make it public? I can only get the data if i'm logged in.


Solution

  • SOLVED IT: I wanted to display the user count so I implemented this code:

    /* Function custom API ROUTE user count*/
    function getUserAmount() {
        $users = get_users(); // Get all wordpress users
    
        if ( empty( $users ) ) { // If there aren't any users display an error
          return 'There aren\'t any users to display.';
        }
        return count($users); // Return amount of users
    }
    /* Preparing to serve an API request */
    add_action( 'rest_api_init', function () {
        register_rest_route( '/wp/v2', '/users', 
            array(
                'methods' => 'GET',
                'callback' => 'getUserAmount',
            )
        );
    } );
    

    Next I opened the /wp-json/wp/v2/users in the Disable REST API plug-in.

    At first I tried using my self-made url: /wp-json/api/wp/colleagues/, but this didn't work, because I couldn't open it in the plug-in, so it was blocked.

    Just using a regular link (/wp-json/wp/v2/users) and returning count() instead of it's normal value, it worked!