Disable the admin bar for specific user - wordpress

I have a WordPress site. There are many users. I want when an author logs in, the author who currently is currently logged on couldn't access the "edit page" menu in the admin bar.
Is there any plugin to disable that?

You can use this plugin :
http://wordpress.org/extend/plugins/admin-bar-disabler/
OR Alternative and manual way is under if condition place this
show_admin_bar(false);
E.g.
if(!is_admin())
{
show_admin_bar(false);
}
place this code in functions.php so that it will disable the admin bar for all the other users.

In your functions.php file, you can add one of the following code snippets to get the indicated results:
// Only display to administrators
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
// Disable for specific role (in this case, 'subscriber')
function remove_admin_bar() {
$user = wp_get_current_user();
if (in_array(‘subscriber’, $user->roles)) {
show_admin_bar(false);
}
}

Related

Hide plugin from admin bar & plugin list

I am trying to hide the following item from the following sections:
Admin bar: ID = wp-admin-bar-nitropack-top-menu
Plugin list: data-slug="nitropack"
I have tried these methods, but can not get it to work. Maybe i have the wrong IDs/Slugs?
Methods: https://divi.space/wordpress-and-divi-code-snippets/hide-any-plugin-from-the-wordpress-dashboard/
Would really appreciate some help, since a customer should not be able to change the settings within this plugin!
Best regards,
Isac
The css way
<style>
a[data-slug="nitropack"] { //hides all a href's with that data slug
display:none;
}
</style>
normally if its an wp admin menu you would do something like this:
//remove admin page item
function edit_admin_menus() {
remove_menu_page("index.php"); //Dashboard
remove_menu_page("itsec"); // wp-admin.php?page=itsec use this "itsec"
}
add_action("admin_menu", "edit_admin_menus");
or you need to remove admin bar item
//remove tool bar item
function remove_toolbar_node($wp_admin_bar) {
// replace 'updraft_admin_node' with your node id "nitropack" something
$wp_admin_bar->remove_node("avia");
$wp_admin_bar->remove_node("updates");
$wp_admin_bar->remove_menu("wp-logo");
$wp_admin_bar->remove_menu("themes");
$wp_admin_bar->remove_menu("widgets");
$wp_admin_bar->remove_menu("dashboard");
//$wp_admin_bar->remove_node("updraft_admin_node");
}
add_action("admin_bar_menu", "remove_toolbar_node", 999);
FYI, since you need to block access to the plugin you'll need to add a redirect based on member role. The customer may know the actual url and can still access the page.
//Admin or Editor role check, if else send to alt url
function block_pages_for_user() {
$blocked_pages = is_page('slug');
$url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if( !current_user_can('administrator') && !current_user_can('editor') && !current_user_can('subscriber') && $blocked_pages ) {
wp_redirect( 'http://www.example.dev/your-page/', 301 );
exit;
}
}
add_action( 'wp', 'block_pages_for_user', 8 );

WordPress - autor privileges issue

This is the contents of functions.php. The code displays for the admin some buttons in the editor. Why are these buttons displayed for the admin, but not displayed for the author?
function spectext_button() {
if (current_user_can('edit_posts') && current_user_can('edit_pages'))
{
add_filter('mce_external_plugins', 'spectext_plugin');
add_filter('mce_buttons_2', 'spectext_register_button');
}
}
add_action('init', 'spectext_button');
function spectext_plugin($plugin_array){
$plugin_array['spectext'] = get_bloginfo('template_url').'/js/newbuttons.js';
return $plugin_array;
}
function spectext_register_button($buttons){
array_push($buttons, "green");
array_push($buttons, "yellow");
array_push($buttons, "red");
return $buttons;
}
&& current_user_can('edit_pages')
The above mentioned code checks if the user has the privileges to edit pages which is allowed to only following user roles:
Super Administrator
Administrator
Editor
If you remove this condition your buttons will work for all users capable of editing posts i.e.
Super Administrator
Administrator
Editor
Author
Contributor

Remove WordPress admin bar on a single page

Is there a way to remove the WordPress admin bar but just on a specific page?
I know that I can remove it or hide it completely by adding an action:
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
I've done that but on my site I have one page that is displayed in an iframe (using colorbox) and I don't ever want the admin bar to show up in that iframe.
Is there a way to hide the admin bar but just for a specific page?
Thanks,
Ben
Your function is a bit complicated, there's a filter for that. Just check for the Page ID and filter it out using the show_admin_bar filter.
function riga_hide_admin_bar(){
if( $post->ID == YOUR_POST_ID ){
return false;
}
}
add_filter( 'show_admin_bar' , 'riga_hide_admin_bar' );
Remove WordPress admin bar on a single page
Yes, you can remove admin bar from a specific page as you wish.
Use add_filter( 'show_admin_bar', '__return_false' ); to hide admin bar from a specific page.
function my_function_admin_bar(){
return false;
}
add_filter( 'show_admin_bar' , 'my_function_admin_bar');
For more details please see below link:
https://codex.wordpress.org/Plugin_API/Filter_Reference/show_admin_bar
Get ID of specific page and add your filter in condition which meet the page ID
`If(get_the_ID()==#736637){
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
}`
For me the easiest way is to add the code in the template of the page I don't want to show. At the top of the template file I just add:
<?php
show_admin_bar(false);

Wordpress 4.3 hide admin bar for non administrators

After updating to Wordpress 4.3 users can see the admin bar. I use this code to hide it normally, but this is not working anymore in 4.3.
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
Any ideas?
The function current_user_can refers to capabilities or user role names. So try manage_options instead:
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
// 'manage_options' is a capability assigned only to administrators
if (!current_user_can('manage_options') && !is_admin()) {
show_admin_bar(false);
}
}
Instead of using the after_setup_theme action you can also add a filter (prefered for newer WP versions):
add_filter( 'show_admin_bar' , 'handle_admin_bar');
function handle_admin_bar($content) {
// 'manage_options' is a capability assigned only to administrators
// here, the check for the admin dashboard is not necessary
if (!current_user_can('manage_options')) {
return false;
}
}
Here is a 6 lines code nugget that will remove the admin bar for non contributor users :
add_action( 'init', 'fb_remove_admin_bar', 0 );
function fb_remove_admin_bar() {
if (!current_user_can('edit_posts')) { // you can change the test here depending on what you want
add_filter( 'show_admin_bar', '__return_false', PHP_INT_MAX );
}
}
Put that in your function.php file or in your custom plugin.
Thanx all for helping. Eventually the problem was a maintenance plugin. When i disabled this it was working again.
Place the code in your theme's functions.php file
if (!current_user_can('manage_options')) {
add_filter('show_admin_bar', '__return_false');
}
Disable the WordPress Admin Bar Using CSS
You only have to copy and paste the CSS code below in Appearance > Customize > Additional CSS, or your style.css file.
The CSS code to disable the toolbar is:
#wpadminbar { display:none !important;}

Disable user profile editing for one user in Wordpress

I have being looking in internet how can I disable the option of edit profile in Wordpress for a user and I did't find a good way. The problem is that I want that the 95% of users can edit their profiles (address, telephone,...) but there is another 5% that I don't want them to have access to the profile section.
The best solution for me is to select manually the users that I want to disable but I also thought about create a role or a group of users to manage it.
There is no plugin for this and the only thing I have found is the following code but it is not working.
add_action('admin_init', 'user_profile_fields_disable');
function user_profile_fields_disable() {
global $pagenow;
// apply only to user profile or user edit pages
if ($pagenow!=='profile.php' && $pagenow!=='user-edit.php') {
return;
}
// do not change anything for the administrator
if (current_user_can('administrator')) {
return;
}
if (current_user_can('custom_role') {
add_action( 'admin_footer', 'user_profile_fields_disable_js' );
}
}
/**
* Disables selected fields in WP Admin user profile (profile.php, user-edit.php)
*/
function user_profile_fields_disable_js() {
?>
<script>
jQuery(document).ready( function($) {
var fields_to_disable = ['email', 'role'];
for(i=0; i<fields_to_disable.length; i++) {
if ( $('#'+ fields_to_disable[i]).length ) {
$('#'+ fields_to_disable[i]).attr("disabled", "disabled");
}
}
});
</script>
<?php
}
You have to substitude the "custom_role" with the name of the role I have assign to the user.
Is that code obsolete or bad? Any idea of how to solve it?
Thanks.

Resources