为什么set_role导致内部服务器错误500?

时间:2019-01-14 16:28:36

标签: php wordpress plugins

我对php和wordpress环境还很陌生。无论如何,我已经着手创建一个插件,该插件使用wp-cron定期更新用户角色。 但是,我想我已经错过了一些东西,因为每当我尝试使用set_role(或add_role)时,都会导致内部服务器错误500。...

我要制作的插件是针对wordpress 5.0.3多站点的,只能在其中一个站点上激活。 在激活和测试插件之前,我使用Justin Tadlock的插件“ Members”创建了自定义角色。 调试时,似乎可以使用户对象具有相应的角色,但是尝试更改其角色会导致服务器错误。

// Get all users with specific role
$all_users_with_role = get_users( 'role=custom_role' );

// Go through users and update role
foreach ( $all_users_with_role as $user ) {

    $user_role = new WP_User( $user->ID ); // This works fine
    $user_role->set_role( 'new_custom_role' ); // This doesn't...
}

代码将显示在循环中执行此操作的用户具有自定义角色:

echo '<span>' . esc_html( $user_role->user_email ) . '</span>';

但是,使用set_role,add_role或remove_role会导致错误。

有什么我想念的吗?是否不知道我要分配的角色?

1 个答案:

答案 0 :(得分:0)

我不允许发表评论,所以我现在在这里发表评论: 您应该获得更多的调试信息,可能会给您提供一些线索,让其他人在这里为您提供帮助。

https://codex.wordpress.org/Debugging_in_WordPress

在调试日志或WP调试输出中查找500服务器错误,并将信息粘贴到此处。

相关问题