我有一个wordpress多站点安装与100+用户的博客。有些用户有多个博客。
用户可以为他们的博客付费(把它想象成一个捐赠场景),或者他们可以选择拥有一个免费的博客。如果他们有一个免费的博客,那么在wp_ blog - id _options中有一个名为is_free_site
的变量,它被设置为1。( blog - id 与用户的blog相关)
如果用户为他们的站点付费,is_free_site
将被设置为0或该变量将在数据库中根本不存在。看到截图:
我正试图编写一个名为does_user_pay
的函数,这将查看当前登录的用户是否为任何的网站付费,如果他们这样做,则返回true。这样我就可以向那些选择付费的人提供优质内容
例如,用户A可能有两个站点,一个他们付费,一个他们不付费-所以does_user_pay()
应该为真
用户B可能有一个他们不付费的站点,so does_user_pay()
将为假
用户C可能有一个他们付费的站点,因此does_user_pay()
将为真。
我正在将其编码为自定义插件,以下是我到目前为止所做的:
function does_user_pay() {
global $current_user;
$user_id = get_current_user_id();
$user_blogs = get_blogs_of_user($user_id);
// Need to write a function here that checks if any of the user blogs are paid for
if(is_user_logged_in() && USER_HAS_PAID_SITE) {
return true;
} else {
return false;
}
}
任何帮助都将非常感激
您可以通过以下方式执行此检查(假设用户已登录并且其id存储在$user_id中):
$blogs = get_blogs_of_user($user_id); // array with all user blogs
foreach($blogs as $blog){
switch_to_blog($blog->userblog_id); // switch the blog
$is_free_site = get_option('is_free_site', 0); // get the option value (if not exists, so the user paid, we'll get 0)
restore_current_blog(); // it's important to restore after a switch blog
if($is_free_site == 0) return true; // found a paid blog
}
return false; // not found a paid blog