Remove the add new item from wordpress but allow editing - wordpress

I have this code that removes pages from users that are not admin on a site I developing.
function remove_menu_items() {
if (!current_user_can('manage_options')){
remove_menu_page( 'index.php' );
remove_menu_page( 'edit-comments.php' );
remove_menu_page( 'edit.php' );
remove_menu_page( 'edit.php?post_type=page' );
remove_menu_page( 'edit.php?post_type=hp_slides' );
remove_menu_page( 'post-new.php?post_type=foodswaps' );
}
}
add_action( 'admin_menu', 'adjust_the_wp_menu', 999 );
However the bottom remove item doesn't work, the post type is correct but the sub menu item still remains. Can any one see what I have done wrong?

I had this problem a couple weeks ago!
So you are trying to remove a submenu item, therefore need to use something like this:
function remove_menu_items() {
if ( ! current_user_can( 'manage_options' ) ) {
// remove new post button from the food swaps custom post type if not admin
$page = remove_submenu_page( 'edit.php?post_type=foodswaps', 'post-new.php?post_type=foodswaps' );
}
}
add_action( 'admin_menu', 'remove_menu_items' );

Related

Wordpress Admin Menu, display an other name for the top level

I am trying to add a different menu title. Like "Goto" should be "More" but if you click on it it should still go to the first.
add_action('admin_menu', 'my_menu_pages');
function my_menu_pages(){
add_menu_page('More', 'More', 'manage_options', 'my-menu', 'edit.php?post_type=goto' );
add_submenu_page('my-menu', 'Goto', 'Goto', 'manage_options', 'edit.php?post_type=goto' );
add_submenu_page('my-menu', 'Gallerys', 'Gallerys', 'manage_options', 'edit.php?post_type=join' );
}
Add the follows code snippet in your active theme's functions.php to achieve the above -
add_action( 'admin_menu', 'fix_admin_menu', 999 );
function fix_admin_menu(){
global $submenu;
if ( ! isset( $submenu['my-menu'] ) ) {
return;
}
unset( $submenu['my-menu'][0] );
}

Completely customize admin menu without plugin

Basically what I want is to remove most of the admin menu and replace it with my own, without using a plugin. But I'd also like it to stay after a wp update.
Is this a possibility and if so, how?
Or is it just a million times easier to make the plugin and be done with it?
EDIT
In /wp-admin there is a menu on the left side. I won't be needing most of the menu and therefor don't want others to see or edit stuff in there(because the only thing that will happen is it will break the site). So I want to remove the unnecesary menu items and add relevant menu items. (It's about Posts, Pages, Media etc)
As others already mentioned you could use the function remove_menu_page, but you still have to put this code somewhere.
If you really don't want to create a plugin you could add this code to the functions.php file of your theme.
In my opinion it's not theme related code, so it would be better to put it in a custom plugin. And it's really easy, here's a video on how to create one within minutes:
https://www.youtube.com/watch?v=S9Nhb1KX7vM
In your case it would look something like:
<?php
/*
Plugin Name: Custom Admin Menu
Version: 1.0.0
Description: My Custom Admin Menu
Author: Peter van der Net
*/
if (!function_exists('my_custom_admin_menu')):
function my_custom_admin_menu(){
remove_menu_page('index.php');
remove_menu_page('plugins.php');
remove_menu_page('users.php');
// etcetera..
}
add_action('admin_menu', 'my_custom_admin_menu');
endif;
/*?>*/
Put this code in a file named custom-admin-menu.php in the folder wp-content/plugins. And then activate the plugin.
although it is not necessary and there are a lot of other ways to secure your wordPress install but still you can refer the below page and this will answer your question
https://codex.wordpress.org/Function_Reference/remove_menu_page
Hope this helps
Take care and Happy coding
This function should be called on the admin_menu action hook.
<?php
function custom_menu_page_removing() {
remove_menu_page( $menu_slug );
}
add_action( 'admin_menu', 'custom_menu_page_removing' );
?>
Removes every menu for all users.
<?php
function remove_menus(){
remove_menu_page( 'index.php' ); //Dashboard
remove_menu_page( 'jetpack' ); //Jetpack*
remove_menu_page( 'edit.php' ); //Posts
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( 'themes.php' ); //Appearance
remove_menu_page( 'plugins.php' ); //Plugins
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' );
?>
For detailed explanation: URL

Is there a way to only show my self-made plugin when a specific Wordpress users logs in? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Is there a way to hide content from a specific WP users? For instance a users logs in with a non admin role. He will only be allowed to see the plugin page.
Create Custom role :
<?php
add_action('init', 'cloneRole');
function cloneRole()
{
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
$adm = $wp_roles->get_role('administrator');
//Adding a 'new_role' with all admin caps
$wp_roles->add_role('new_role', 'My Custom Role', $adm->capabilities);
}
?>
User(My Custom Role) can see only plugin menu while login :
<?php
function remove_menus(){
if( get_user_role() == 'My Custom Role')
remove_menu_page( 'index.php' ); //Dashboard
remove_menu_page( 'jetpack' ); //Jetpack*
remove_menu_page( 'edit.php' ); //Posts
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( 'themes.php' ); //Appearance
// remove_menu_page( 'plugins.php' ); //Plugins
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' );
?>
function get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
You can go for any plugin
You need to use the right hooks (which are not always the same as the URLs/slugs), and it doesn't hurt to use a hook that runs later (e.g., admin_init):
add_action( 'admin_init', 'wpse_136058_remove_menu_pages' );
function wpse_136058_remove_menu_pages() {
remove_menu_page( 'edit.php?post_type=acf' );
remove_menu_page( 'wpcf7' );
}
You can use the following to debug:
add_action( 'admin_init', 'wpse_136058_debug_admin_menu' );
function wpse_136058_debug_admin_menu() {
echo '<pre>' . print_r( $GLOBALS[ 'menu' ], TRUE) . '</pre>';
}
This gives (for my setup) the following for the Contact Form 7 plugin menu page:
[27] => Array
(
[0] => Formular
[1] => wpcf7_read_contact_forms
[2] => wpcf7
[3] => Contact Form 7
[4] => menu-top menu-icon-generic toplevel_page_wpcf7 menu-top-last
[5] => toplevel_page_wpcf7
[6] => none
)
The array element with key 2 is what you are looking for: wpcf7.

Remove submenu page "customize.php" in WordPress 4.0

When I was running WP 3.9.2 I was able to use the following code to remove the Customize menu item from Appearance in the admin menu.
function remove_customize() {
remove_submenu_page('themes.php', 'customize.php');
}
add_action('admin_init', 'remove_customize', 999);
Once I updated to 4.0 this is no longer working.
This works with WordPress 4.1 and 4.0 and 3.x here:
Edit: Adjusted for WordPress 4.1 compatibility:
function remove_customize() {
$customize_url_arr = array();
$customize_url_arr[] = 'customize.php'; // 3.x
$customize_url = add_query_arg( 'return', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'customize.php' );
$customize_url_arr[] = $customize_url; // 4.0 & 4.1
if ( current_theme_supports( 'custom-header' ) && current_user_can( 'customize') ) {
$customize_url_arr[] = add_query_arg( 'autofocus[control]', 'header_image', $customize_url ); // 4.1
$customize_url_arr[] = 'custom-header'; // 4.0
}
if ( current_theme_supports( 'custom-background' ) && current_user_can( 'customize') ) {
$customize_url_arr[] = add_query_arg( 'autofocus[control]', 'background_image', $customize_url ); // 4.1
$customize_url_arr[] = 'custom-background'; // 4.0
}
foreach ( $customize_url_arr as $customize_url ) {
remove_submenu_page( 'themes.php', $customize_url );
}
}
add_action( 'admin_menu', 'remove_customize', 999 );
Answer should be:
add_action( 'admin_menu', function () {
global $submenu;
if ( isset( $submenu[ 'themes.php' ] ) ) {
foreach ( $submenu[ 'themes.php' ] as $index => $menu_item ) {
foreach ($menu_item as $value) {
if (strpos($value,'customize') !== false) {
unset( $submenu[ 'themes.php' ][ $index ] );
}
}
}
}
});
The way rjb used an array as the needle in in_array() in the accepted answer doesn't work. Check out why in the docs. I replaced in_array with another foreach that loops through the $menu_item arrays and looks for 'customize' as part of the value.
Works for me with WordPress 4.9.6
You can directly modify the $submenus global like so:
global $submenu;
unset($submenu['themes.php'][6]); // Customize link
I'm using this in the same function, hooked into admin_menu, as I use to unset other admin items and it seems to be working fine
function as_remove_menus () {
remove_menu_page('upload.php'); //hide Media
remove_menu_page('link-manager.php'); //hide links
remove_submenu_page( 'edit.php', 'edit-tags.php' ); //hide tags
global $submenu;
// Appearance Menu
unset($submenu['themes.php'][6]); // Customize
}
add_action('admin_menu', 'as_remove_menus');
Edit: Updated for WordPress 4.9+ and increased compatibility with PHP <= 5.4
WordPress core doesn't offer a hook to natively disable the theme customizer, but there is a clever and elegant way to remove the “Customize” link from the Appearance menu by altering the global $submenu variable:
/**
* Remove Admin Menu Link to Theme Customizer
*/
add_action( 'admin_menu', function () {
global $submenu;
if ( isset( $submenu[ 'themes.php' ] ) ) {
foreach ( $submenu[ 'themes.php' ] as $index => $menu_item ) {
if ( in_array( array( 'Customize', 'Customizer', 'customize' ), $menu_item ) ) {
unset( $submenu[ 'themes.php' ][ $index ] );
}
}
}
});
While other code samples here and elsewhere irresponsibly rely on specific numeric indexes of the global $submenu variable (e.g. $submenu['themes.php'][6][0], ...), this method intelligently traverses through the hierarchy so it should be compatible with older (3.x) and newer versions of WordPress (4.x) alike.
You can in fact use remove_submenu_page to remove the theme submenu option from the admin screen. The trick is that the url must match what is being exactly linked in the admin for that function to work.
function remove_admin_menus() {
remove_submenu_page(
'themes.php',
'customize.php?return=' .
urlencode( str_replace( get_bloginfo('url'), "", get_admin_url() ) ) .
'themes.php' );
}
add_action( 'admin_init', 'remove_admin_menus' );
I have programmatically determined the admin url in the case that you aren't simply using '/wp-admin'. #isabisa This will also avoid breaking in the future if the index of the menu item ever changes.
I'm using this in WP 4.0 and it works great!
Removing the menu is only a halfway solution, since it doesn't completely disable the customizer. In order to fully and securely disable the customizer (and also remove the menu), you need to remove the customizer permission from all users. Something like this would do it:
add_filter('map_meta_cap', function($caps, $cap, $user_id, $args) {
if ('customize' == $cap) return ['do_not_allow'];
return $caps;
}, 10, 4);
WordPress >= 4.9.8
add_action('admin_menu', function () {
$request = urlencode($_SERVER['REQUEST_URI']);
remove_submenu_page('themes.php', 'customize.php?return='. $request);
}, 999);
The accepted answer by #rjb didn't work for my spanish wordpress, but just changing the Customize to customize did the trick.
/**
* Remove Admin Menu Link to Theme Customizer
*/
add_action( 'admin_menu', function () {
global $submenu;
if ( isset( $submenu[ 'themes.php' ] ) ) {
foreach ( $submenu[ 'themes.php' ] as $index => $menu_item ) {
if ( in_array( 'customize', $menu_item ) ) {
unset( $submenu[ 'themes.php' ][ $index ] );
}
}
}
});
Works in wordpres 5.*
Removing Customize from Wordpress Admin you need to remove from the sidebar and from the top bar in front end as well
From the Sidebar Menu
add_action( 'admin_menu', 'remove_customize' );
function remove_customize() {
global $submenu;
if ( isset( $submenu[ 'themes.php' ] ) ) {
foreach ( $submenu[ 'themes.php' ] as $index => $menu_item ) {
if(in_array('Customize', $menu_item) || in_array('Customizer', $menu_item) || in_array('customize', $menu_item))
{
unset( $submenu[ 'themes.php' ][ $index ] );
}
}
}
}
From Admin bar in top (In The front End)
add_action( 'admin_bar_menu', 'remove_customize_menu_bar', 999 );
function remove_customize_menu_bar( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'customize' );
}
This will completely disable the customize option :)
For WordPress 5
add_action( 'admin_menu', function() {
remove_submenu_page( 'themes.php', 'customize.php?return=' . urlencode($_SERVER['SCRIPT_NAME']));
}, 999 )
Try changing 'admin_init' in 'admin_menu'
#bash88 answer and #Emanuel A. answer works but if you want also remove buttons (blue customize buttons) from themes page answer should be:
Tested WordPress 5.0.3
/**
* Remove customize links from admin panel.
*/
function admin_remove_customize_links() {
echo '<style>.hide-if-no-customize { display: none !important; }</style>';
}
add_action( 'admin_head', 'admin_remove_customize_links' );
Update of the approved response (Wordpress 5)
add_action( 'admin_menu', 'rompiot_remove_customize' );
/**
* Remove Admin Menu Link to Theme Customizer
*/
public function rompiot_remove_customize()
{
global $submenu;
if (isset($submenu['themes.php'])) {
foreach ($submenu['themes.php'] as $index => $array_menu_item) {
foreach ($array_menu_item as $key => $menu_item) {
if (in_array($menu_item, ['Customize', 'Customizer', 'customize'])) {
unset($submenu['themes.php'][$index]);
}
}
}
}
}
FUNCTIONING CODE AS OF WORDPRESS 5.7+, AUGUST 2021
I needed to customize the WP admin bar for a large WP site we are working on (text was running off the admin bar when resizing). I tried literally every snippet of code from all the previous answers to this question; unfortunately none worked for me. This code I found off Google did, so I wanted to share with others (goes in your functions.php):
/**
* This function removes items from the WP admin bar. If it gets too cluttered,
* things will run off the screen and look bad.
* #param object $wp_admin_bar representing the WP admin bar.
*/
function remove_from_admin_bar($wp_admin_bar) {
// WordPress Core Items (uncomment to remove)
$wp_admin_bar->remove_node('updates');
$wp_admin_bar->remove_node('comments');
//wp_admin_bar->remove_node('new-content');
$wp_admin_bar->remove_node('wp-logo');
//$wp_admin_bar->remove_node('site-name');
//$wp_admin_bar->remove_node('my-account');
//$wp_admin_bar->remove_node('search');
$wp_admin_bar->remove_node('customize');
}
add_action('admin_bar_menu', 'remove_from_admin_bar', 999);
I think a great start is to remove the comments, updates, and WP icon. Custom plugins can be disabled here, too. The 999 indicates when the hook will fire off later. You can wrap elements in the is_admin() function if you want to hide or show different links on the front end vs. the WP admin.

Restrict registered users from altering their own biographical info

I want to restrict all users from altering their own 'Biographical Info' (only I the admin should be able to edit/update it) in Dashboard->Users->Your Profile.
You can do that with following;
<?php
add_action( 'admin_init', 'disable_profile_edit' );
function disable_profile_edit() {
remove_menu_page( 'profile.php' );
remove_submenu_page( 'users.php', 'profile.php' );
if(IS_PROFILE_PAGE === true && ! current_user_can( 'manage_options' )) {
wp_redirect( home_url() );
exit;
}
}
?>
You can put only code part to functions.php. If anyone (except the users has role manage_options) tries to access profile page, it will be denied.
Note:
Put ;
define('IS_PROFILE_PAGE', true);
in profile.php

Resources