Last active
November 5, 2023 23:29
-
-
Save leoloso/4e367eb8d8014a7aa7580567608bd5b4 to your computer and use it in GitHub Desktop.
WordPress plugin - Disable user edit profile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Helper Plugin - Disable user edit profile for WP PoP API | |
Version: 0.1 | |
Description: It removes the edit profile page for all users other than admins | |
Plugin URI: https://gist.github.com/leoloso/4e367eb8d8014a7aa7580567608bd5b4 | |
Author: Leonardo Losoviz | |
*/ | |
/** | |
* @see: https://wordpress.stackexchange.com/a/45892 | |
*/ | |
// ===== remove edit profile link from admin bar and side menu and kill profile page if not an admin | |
function mytheme_admin_bar_render() { | |
if( current_user_can('activate_plugins') ) { | |
return; | |
} | |
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( current_user_can('activate_plugins') ) { | |
return; | |
} | |
if(defined('IS_PROFILE_PAGE') && 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' ); |
@kalexgo I guess you're logged-in not as an admin but maybe writer or subscriber, that's why you see that message. You need to have the activate_plugins
role permission to access your profile page (admins do):
function stop_access_profile() {
if( current_user_can('activate_plugins') ) {
return;
}
if(defined('IS_PROFILE_PAGE') && 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' );
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Leo. I just activated your plugin which is exactly what I have been looking for but it's showing an error message 'Please contact your administrator to have your profile information changed.' What do I need to do, please?