drkxgs9358 2018-12-12 14:51
浏览 46
已采纳

wordpress从其他网站获取用户数

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, dus making it dynamic?

Thanks for all the replies!

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.

  • 写回答

1条回答 默认 最新

  • dpkiubcc265650 2018-12-14 12:44
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题