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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?