問題描述

我正在開發一個內部網,我正在使用 Justin Tadlock 的 Members 外掛來控制角色和功能。

我建立了一個 HR 角色,允許人力資源人員建立和編輯使用者帳戶。在 WP 中建立的所有員工都獲得了 contributor 角色,併為 editoradministrator 角色提供了一些選定的員工。

我想要的是阻止員工登入和更改自己的個人資料資訊。只有 HR 角色的工作人員才能編輯個人資料資訊。

最佳解決辦法

非常好的答案,讓我們進一步瞭解,任何人都希望將其應用於所有 non-admin 使用者 (例如,貢獻者,編輯者等)

// ===== 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' );
}

次佳解決辦法

用一點時間做好了。這是我正在使用的程式碼:

<?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.' );
    }
}
?>

上述程式碼阻止任何人編輯自己的個人資料資訊,儘管他們是誰。有能力建立和編輯用途的人仍然可以這樣做,但不能改變自己的。

第三種解決辦法

解決方案作為 (MU-) 外掛

我檢查了所有提供的解決方案,並認為我可以做出一個很好的 MU-Plugin 。唯一真正的變化是它避免了

<?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' );

第四種辦法

上述所有解決方案都使用常數:IS_PROFILE_PAGE

if(IS_PROFILE_PAGE === true) {

但是,如果 wordpress 除錯設定為 true,它將丟擲”undefined constant” 錯誤。要解決這個問題 :

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

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。