禁止用户编辑自己的配置文件信息

时间:2011-09-20 作者:Scott

我目前正在开发一个intranet,我正在使用JustinTadlock的成员插件来控制角色和功能。

我创建了一个HR 角色,允许人力资源人员创建和编辑用户帐户。WP中创建的所有员工都有contributor 指定了部分员工的角色editoradministrator 角色。

我想要的是阻止员工登录并更改他们自己的个人资料信息。仅限HR 角色应该能够编辑配置文件信息。

5 个回复
最合适的回答,由SO网友:Scott 整理而成

花了一点时间就解决了。以下是我使用的代码:

<?php
/*
Plugin Name: Restrict User Editing Own Profile
Plugin URI: http://www.philosophydesign.com
Description: Restricts users from editing their own profile information.
Author: Scott Cariss
Version: 0.1
Author URI: http://www.philosophydesign.com/scott-cariss.html
*/

add_action( \'admin_menu\', \'stop_access_profile\' );
function stop_access_profile() {
    remove_menu_page( \'profile.php\' );
    remove_submenu_page( \'users.php\', \'profile.php\' );
    if(IS_PROFILE_PAGE === true) {
        wp_die( \'You are not permitted to change your own profile information. Please contact a member of HR to have your profile information changed.\' );
    }
}
?>
上述代码阻止任何人编辑自己的个人资料信息,无论他们是谁。有能力创建和编辑用途的人仍然可以这样做,但不能更改自己的用途。

SO网友:sirG

很好的回答,这是一个更进一步的步骤,适用于任何希望将此应用于所有非管理员用户(例如,贡献者、编辑等)的人

// ===== remove edit profile link from admin bar and side menu and kill profile page if not an admin
if( !current_user_can(\'activate_plugins\') ) {
function mytheme_admin_bar_render() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu(\'edit-profile\', \'user-actions\');
}
add_action( \'wp_before_admin_bar_render\', \'mytheme_admin_bar_render\' );

function stop_access_profile() {
    if(IS_PROFILE_PAGE === true) {
        wp_die( \'Please contact your administrator to have your profile information changed.\' );
    }
    remove_menu_page( \'profile.php\' );
    remove_submenu_page( \'users.php\', \'profile.php\' );
}
add_action( \'admin_init\', \'stop_access_profile\' );
}

SO网友:kaiser

作为(MU)插件的解决方案我检查了所有提供的解决方案,并认为我可以用它制作一个很好的MU插件。唯一真正的改变是它避免了

<?php
! defined( \'ABSPATH\' ) AND exit;
/**
 * Plugin Name: Disable profile page link
 * Description: Remove edit profile link from admin bar and side menu and kill profile page if user isn\'t an administrator.
 */
# Version: 2012-09-15.2245

function oxo_stop_access_profile()
{
    // Remove AdminBar Link
    if ( 
        \'wp_before_admin_bar_render\' === current_filter()
        AND ! current_user_can( \'manage_options\' )
    )
        return $GLOBALS[\'wp_admin_bar\']->remove_menu( \'edit-profile\', \'user-actions\' );

    // Remove (sub)menu items
    remove_menu_page( \'profile.php\' );
    remove_submenu_page( \'users.php\', \'profile.php\' );

    // Deny access to the profile page and redirect upon try
    if ( 
        defined( \'IS_PROFILE_PAGE\' )
        AND IS_PROFILE_PAGE
        AND ! current_user_can( \'manage_options\' )
        )
    {
        wp_redirect( admin_url() );
        exit;
    }
}
add_action( \'wp_before_admin_bar_render\', \'oxo_stop_access_profile\' );
add_action( \'admin_menu\', \'oxo_stop_access_profile\' );

SO网友:sudip

以上所有解决方案都使用常量:IS\\u PROFILE\\u页面

if( IS_PROFILE_PAGE === true ) {
但是,如果wordpress debug设置为true,它将抛出“undefined constant”错误。要修复它,请执行以下操作:

if( defined(\'IS_PROFILE_PAGE\') && IS_PROFILE_PAGE === true ){
........................
}

SO网友:Adiyya Tadikamalla

add_action( \'admin_menu\', \'prefix_disable_profile_access\' );
function prefix_disable_profile_access() {
    if( ! current_user_can(\'editor\') || ! current_user_can(\'administrator\') ) { // You can add the user roles who can edit their profiles here.
        remove_menu_page( \'profile.php\' );
        remove_submenu_page( \'users.php\', \'profile.php\' );
        if ( true === IS_PROFILE_PAGE ) {
            wp_die( \'You are not permitted to change your own profile information. Please contact a member of HR to have your profile information changed.\' );
        }
    }
}
希望这对一些人有所帮助。

结束