Turning off multiple WordPress menu items for email addresses - php

I have the below function, but don't know how to add multiple email addresses to the list.
// Remove Different Admin Menu Links For A Specific User Profile
add_action('admin_menu', 'remove_admin_menu_links');
function remove_admin_menu_links(){
$user = wp_get_current_user();
if( $user && isset($user->user_email) && 'user#email.com' == $user->user_email ) {
remove_menu_page( 'tools.php' );
remove_menu_page( 'themes.php' );
remove_menu_page( 'options-general.php' );
remove_menu_page( 'plugins.php' );
remove_menu_page( 'users.php' );
remove_menu_page( 'edit-comments.php' );
remove_menu_page( 'page.php' );
remove_menu_page( 'upload.php' );
remove_menu_page( 'edit.php?post_type=page' );
remove_menu_page( 'edit.php?post_type=videos' );
remove_menu_page( 'edit.php' );
}
}
remove_theme_support( 'genesis-admin-menu' );

Create an array of email addresses and check for their existence:
add_action('admin_menu', 'remove_admin_menu_links');
function remove_admin_menu_links(){
$restrictedUsers = [
"someone#somewhere.com",
"user#email.com"
];
$user = wp_get_current_user();
if( $user && isset($user->user_email) && in_array($user->user_email, $restrictedUsers )) {
remove_menu_page( 'tools.php' );
remove_menu_page( 'themes.php' );
remove_menu_page( 'options-general.php' );
remove_menu_page( 'plugins.php' );
remove_menu_page( 'users.php' );
remove_menu_page( 'edit-comments.php' );
remove_menu_page( 'page.php' );
remove_menu_page( 'upload.php' );
remove_menu_page( 'edit.php?post_type=page' );
remove_menu_page( 'edit.php?post_type=videos' );
remove_menu_page( 'edit.php' );
}
}
remove_theme_support( 'genesis-admin-menu' );

Related

Hide wp menu items based on user role / id / etc

i've tried the following to hide a few wp menu items from my client dashboard:
// Hide admin menu items for shop manager
function hide_menu() {
if (current_user_can('manage_woocommerce')) {
/* WP DEFAULT MENUS */
remove_menu_page( 'edit.php' ); //Posts
remove_menu_page( 'edit.php?post_type=page' ); //Pages
remove_menu_page( 'edit-comments.php' ); //Comments
remove_menu_page( 'themes.php' ); //Appearance
remove_menu_page( 'users.php' ); //Users
remove_menu_page( 'tools.php' ); //Tools
/* PLUGINS */
remove_menu_page( 'edit.php?post_type=elementor_library&tabs_group=library' ); //Template
remove_menu_page( 'admin.php?page=yith_wcan_panel' ); //Yith
}
}
add_action('admin_head', 'hide_menu', 5 );
But now the admin menu items are gone for me as well (superuser).
What did i get wrong?
Also, while removing things also for me while it shouldn't, it didn't remove the WC submenu items "Settings" and "Addons", nor elementor's "Template" and Yith tab (see attachment).
EDIT
now i've tried this:
// Hide admin menu items for shop manager
function hide_menu() {
if ( !current_user_can('manage_options') ) {
/* WP DEFAULT MENUS */
remove_menu_page( 'edit.php' ); //Posts
remove_menu_page( 'edit.php?post_type=page' ); //Pages
remove_menu_page( 'edit-comments.php' ); //Comments
remove_menu_page( 'themes.php' ); //Appearance
remove_menu_page( 'users.php' ); //Users
remove_menu_page( 'tools.php' ); //Tools
/* PLUGINS */
remove_submenu_page( 'admin.php?page=wc-admin', 'admin.php?page=wc-settings'); // Settings
remove_submenu_page( 'admin.php?page=wc-admin', 'admin.php?page=wc-addons'); // Addons
remove_menu_page( 'edit.php?post_type=elementor_library&tabs_group=library' ); //Template
remove_menu_page( 'admin.php?page=yith_wcan_panel' ); //Yith
}
}
add_action('admin_head', 'hide_menu', 5 );
And it mostly works, however i'm still not able to hide the two woocommerce subpages (settings and marketplace), elementor's template and yith.
Change your conditional to if (current_user_can('manage_woocommerce') && ! is_admin()) {
Depending on your site you may need to use is_super_admin()

Show Admin Panel pages on WordPress depending on User logged in

I have a function below that hides the menus in the admin panel - Since I am the developer of this website, I don't want the admins of the company to see certain sections such a plugins, etc.. How would I make it so that let's say 'Show it to only user called "Test"'.
function remove_menus() {
/* remove_menu_page( 'index.php' ); //Dashboard
remove_menu_page( 'edit.php' ); //Posts */
remove_menu_page( 'layerslider' );
remove_menu_page( 'themes.php' ); //Appearance
remove_menu_page( 'plugins.php' ); //Plugins
/* remove_menu_page( 'upload.php' ); //Media
remove_menu_page( 'edit.php?post_type=page' ); //Pages
remove_menu_page( 'edit-comments.php' ); //Comments
remove_menu_page( 'users.php' ); //Users
remove_menu_page( 'tools.php' ); //Tools
remove_menu_page( 'options-general.php' ); //Settings */
}
add_action( 'admin_menu', 'remove_menus' );
try with this:
function remove_menus() {
$currentUserId = get_current_user_id();
if ($currentUserId != your id) {
remove_menu_page( 'layerslider' );
....
}
}
add_action( 'admin_menu', 'remove_menus' );
So if it's not the desired user, remove pages.

Edit the default rss url of wordpress

I need to change the default rss url of my website:
from example.com/feed to example.com/MyfeedName
Update:
what i tried so far is to create another Url feed but i need to remove firstexample.com/feed:
add_action( 'init', function()
{
add_feed( 'secretfeed', 'do_feed_rss2' );
});
add_action( 'pre_get_posts', function( \WP_Query $q )
{
if( $q->is_feed( 'secretfeed' ) )
add_filter( 'option_rss_use_excerpt', '__return_false' );
} );
do you have any idea how to just edit example.com/feed or how to delete it without losing rss functions ?
I found my answer here :
https://wordpress.stackexchange.com/a/214883/71314
function remove_feed( $feedname ) {
global $wp_rewrite;
if ( in_array( $feedname, $wp_rewrite->feeds ) ) {
$wp_rewrite->feeds = array_diff( $wp_rewrite->feeds, array( $feedname ) );
}
$hook = 'do_feed_' . $feedname;
// Remove default function hook
remove_all_actions( $hook );
add_action( $hook, $hook );
return $hook;
}
Usage:
remove_feed( 'feed' );

remove_menu_page() not getting initialized within if condition

I am trying to remove some menu pages based on user role, but when I add the function inside the if condition it doesn't do anything.
function contributor_posts_action() {
if ($role == 'contributor_posts') { // contributor_posts - custom role
// echo 'here'; for testing purposes and WORKS, so it goes under the if condition
add_action( 'admin_menu', 'remove_menus_contrib' );
function remove_menus_contrib(){
remove_menu_page( 'edit-comments.php' );
remove_menu_page( 'tools.php' );
remove_menu_page( 'edit.php?post_type=directory' );
remove_menu_page( 'edit.php?post_type=city' );
} // this function doesn't get hooked
add_action( 'admin_bar_menu', 'remove_admin_bar_items', 999 );
function remove_admin_bar_items( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'new-directory' );
$wp_admin_bar->remove_node( 'new-city' );
}// this one works properly. It's for removing for admin bar.
}
}
add_action( 'admin_init', 'contributor_posts_action' );
Try pulling the remove_menus_contrib() and the add_action( 'admin_menu', 'remove_menus_contrib' ) hook function out of your contributor_posts_action() function.
Some Wordpress hooks won't work inside other (custom) functions.

PHP function reference using args

I'm new to PHP. I'm trying to hide certain dashboard nav items from certain users (editors). I've added this to the functions, which has hidden it for all users:
<?php
function remove_menus(){
remove_menu_page( 'edit-comments.php' ); //Comments
}
add_action( 'admin_menu', 'remove_menus' );
?>
It says here you can use 'current_user_can' to pinpoint certain users but I'm unsure how to use both together.
So far I've tried:
function remove_menus(){
current_user_can(
remove_menu_page( 'editor', 'edit-comments.php' ); //Comments
) );
}
and
function remove_menus(){
current_user_can( array(
remove_menu_page( 'editor', 'edit-comments.php' ); //Comments
) );
}
..but from looking at other functions, they seem to in brackets with => inbetween so I'm presuming I'm using this function the wrong way.
Any help would be appreciated, thanks.
First answer, very simple , use logic "OR" operator:
<?php
function remove_menus(){
if( current_user_can('editor') || current_user_can('administrator') ) { // stuff here for admins or editors
remove_menu_page( 'edit-comments.php' ); //stuff here for editor and administrator
}
} ?>
If you want to check more than two roles, you can check if the roles of current user is inside an array of roles, something like:
<?php
function remove_menus(){
$user = wp_get_current_user();
$allowed_roles = array('editor', 'administrator', 'author');
if( array_intersect($allowed_roles, $user->roles ) ) {
remove_menu_page( 'edit-comments.php' ); //stuff here for allowed roles
}
} ?>
However, current_user_can can be used not only with users role name, but also with capabilities. So, once both editors and administrators can edit pages, your life can be easier checking for that capabilities:
<?php
function remove_menus(){
if( current_user_can('edit_others_pages') ) {
remove_menu_page( 'edit-comments.php' );// stuff here for user roles that can edit pages: editors and administrators
}
}
?>
Have a look here for more info on capabilities.

Categories