wp_update_user not updating

时间:2012-07-15 作者:Clearmedia

在绝望中,我也在这个论坛上寻求帮助——如果有人(任何人!)如果你能看看这篇文章,看看他们是否能帮上忙,我将永远感激你

http://wordpress.org/support/topic/wp_update_user-not-updating?replies=11

基本上-我正在通过脚本更新用户的角色和功能,但只有当我进入该用户并点击“保存”时,更改才会生效(即用户可以看到特定的菜单项)

期待您的光临!!

克里斯

UPDATE:

也许这有助于解决问题?

This is the WP_User object I have echoed out on screen when logged in as the newly changed user:

WP_User Object ( [data] => stdClass Object ( [ID] => 130 [user_login] => test [user_pass] => $P$BuHO1ABLCNQ716tktgyes4jqqkfVxG. [user_nicename] => test [user_email] => [email protected] [user_url] => [user_registered] => 2012-07-19 12:07:52 [user_activation_key] => [user_status] => 0 [display_name] => test ) [ID] => 130 [caps] => Array ( [editor] => 1 ) [cap_key] => wp_capabilities [roles] => Array ( [0] => editor ) [allcaps] => Array ( [upload_files] => 1 [unfiltered_html] => 1 [edit_posts] => 1 [edit_published_posts] => 1 [publish_posts] => 1 [edit_pages] => 1 [read] => 1 [level_7] => 1 [level_6] => 1 [level_5] => 1 [level_4] => 1 [level_3] => 1 [level_2] => 1 [level_1] => 1 [level_0] => 1 [edit_published_pages] => 1 [publish_pages] => 1 [manage_options] => 1 [view_menu] => 1 [editor] => 1 ) [filter] => )

This is how the menu item is being created in my plugin file (which should display to the user):

add_menu_page(\'Welcome\', \'Welcome\',\'edit_posts\', \'welcome\', \'welcome_page\', get_bloginfo(\'template_url\').\'/images/icon.png\', 0);

This is the function / page that the menu item returns:

function welcome_page()
 {
    global $currrent_user;
if(!current_user_can(\'edit_posts\'))
{

    print \'<div class="wrap"><h2>Your account has been restricted, most likely due to an unpaid subscription.</div>\';
}
else
{
    include \'welcome-page.php\';
}
 }
如您所见,用户只需要edit\\u posts功能即可查看菜单项并返回欢迎页面。用户确实有这个功能,但不能做这两件事-除非我在用户编辑页面中单击“另存为管理员”??

Edit 2

function welcome_page()
 {
    global $currrent_user;
if(!current_user_can(\'edit_posts\'))
{

    print \'<div class="wrap"><h2>Your account has been restricted, most likely due to an unpaid subscription.</div>\';
}
else
{
    include \'welcome-page.php\';
}
 }

UPDATE AGAIN!

我刚刚尝试了这一点,然后又一次,数据库被更新了,甚至在管理面板中显示“编辑器”,但是用户仍然看不到相应的菜单项,除非我在他们的个人资料上单击“更新”!

$new = new WP_User($current_user->ID);
$new->set_role(\'editor\');

wp_cache_delete( $new->ID, \'users\' );
wp_cache_delete( $new->user_login, \'userlogins\' );
wp_cache_delete( $new->user_email, \'useremail\' );
wp_cache_delete( $new->user_nicename, \'userslugs\' );
do_action(\'profile_update\');
有什么想法吗?

3 个回复
SO网友:Pontus Abrahamsson

Im使用remove_role 然后add_role 将用户从一个角色升级到另一个角色。

下面是一个检查用户角色中所有用户的函数subscriber 并将其升级到editor 每小时。

/**
* Add a cron job that will update
* Subscribers to editors. Runs hourly
*/


add_action(\'check_user_role\', \'upgrade_user\');

function run_check_user_role() {
    if ( !wp_next_scheduled( \'check_user_role\' ) ) {
        wp_schedule_event( current_time( \'timestamp\' ), \'hourly\', \'check_user_role\');
    }
}
add_action( \'wp\', \'run_check_user_role\' );



function upgrade_user() {

    // Get users in subscriber role
    $args = array(
        \'role\'  =>  \'subscriber\',
    );

    $users = get_users( $args );

    foreach ( $users as $user ) {

        $user = new WP_User( $user->ID );

        // Remove current subscriber role
        $user->remove_role( \'subscriber\' );

        // Upgrade to editor role
        $user->add_role( \'editor\' );

    }
}
以下是一种手动升级用户的方法:

Run this as http://yourdomain.com/?upgrade_user

if ( isset( $_REQUEST[\'upgrade_user\'] ) ) {
    upgrade_user();
} 

SO网友:Barry Carlyon

我最好的方法是。它使用WP\\u User切换/设置角色。

$role = \'editor\';
$user = new WP_User($user_id);
$user->set_role($role);

SO网友:Son Pham

如果您使用其他插件,则需要阅读有关如何设置角色的文档。在我的情况下,我使用了Ultimate Member,因此我必须添加以下行:

UM()->roles()->set_role( $user_id, $user_role );
//and then clear the user cache after updating the role:
UM()->user()->remove_cache( $user_id );

结束

相关推荐