Disable the admin bar for all users except admin - wordpress

I have installed WordPress and BudyPress. I want to disable the admin bar which appears on the top for all users.
Can somebody tell me how to do that correctly?

function is_current_user_administrator() {
global $current_user;
return !in_array( 'administrator', $current_user->roles );
}
add_filter( 'show_admin_bar', 'is_current_user_administrator' );

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);
}
}

Related

How to hide and disable Wordpress admin menu items

I have two Wordpress Administrator accounts but I need to hide certain admin menu items for one admin. I've out found how to do this but how can I disable directly accessing the page by url? Something like this Advanced Access Manger
So far I have..
function hide_menu() {
$user = wp_get_current_user();
if($user && isset($user->user_login) && 'admin2' == $user->user_login) {
remove_menu_page( 'plugins.php' ); //Plugins
remove_menu_page( 'tools.php' ); //Tools
remove_menu_page( 'users.php' ); //Users
}
}
add_action('admin_head', 'hide_menu');
Easy Solution:
You can use this plugin:
https://wordpress.org/plugins/prevent-direct-access/
Coding Solution:
add_action('template_redirect', function() {
// ID of the page you want to restrict from
if (!is_page(2072)) {
return;
}
// Provide the link of admin dashboard, so that page should only
// accessible
// through admin dashboard
if (wp_get_referer() === 'https://example.com/wp-admin/') {
return;
}
// we are on restricted page
// visitor is trying to access directly
// so redirect to home
wp_redirect(get_home_url());
exit;
} );

Remove dashbord acess form wordpress admin

i have a wordpress website and it have multiple admin. I want to remove dashboard acess of one admin without changing his role .
I need to change dashboard access via a code . And that admin id is 8
So to hide admin bar i use the following code
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (get_current_user_id()==8) {
show_admin_bar(false);
}
}
Now i want to remove his dashboard access . Please help .
You can try below code
function prevent_adminuser_access(){
if((get_current_user_id()==8) && is_admin() ) {
// maybe redirect to homepage
wp_safe_redirect( get_bloginfo( 'url' ) );
}
}
add_action( 'admin_init', 'prevent_adminuser_access' );

How to add CSS only to the options page of a plugin in WordPress?

I want to add a stylesheet for the options_page of my plugin only. But how to do that? My code so far:
function add_options_page_style() {
wp_register_style('options_page_style', plugins_url('css/options_style.css',__FILE__));
wp_enqueue_style('options_page_style');
}
add_action( 'admin_menu', 'add_options_page_style' );
I could place an if statement before the line with add_action... but I'm not sure how to filter my options page. I already tried the $pagename variable and also this line: $wp_query->queried_object->post_name; but it didn't work.
The filter $_GET['page'] does work but might break in future versions.
Somewhere you'll be registering page like this:-
function register_page(){
global $page_hook_suffix;
$page_hook_suffix = add_options_page('Your_plugin', 'Your_plugin', 'manage_options', __FILE__, 'display_form');
}
add_action('admin_menu', 'register_page');
And while enqueueing script you'll do something like this:-
function my_enqueue($hook) {
global $page_hook_suffix;
if( $hook != $page_hook_suffix )
return;
wp_register_style('options_page_style', plugins_url('css/options_style.css',__FILE__));
wp_enqueue_style('options_page_style');
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );
function load_custom_wp_admin_style($hook) {
// Load only on ?page=mypluginname
if($hook != 'toplevel_page_mypluginname') {
return;
}
wp_enqueue_style( 'custom_wp_admin_css', plugins_url('admin-style.css', __FILE__) );
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );
add your page slug as suffix to toplevel_page_
e.g. if page slug is this-plugin-options
then
if($hook != 'toplevel_page_this-plugin-options') {
return;
}
here is wordpress doc with
Example: Load CSS File on All Admin Pages,
Example: Load CSS File from a plugin on specific Admin Page

Hiding Page attributes on page editor from users below admin levels

is there any way to hide Page attributes from users below admin levels?
(for an example, i dont want that user with editor level, will be able to change page template).
thank you
You may try this
function remove_page_attribute_meta_box()
{
if( is_admin() ) {
if( current_user_can('editor') ) {
remove_meta_box('pageparentdiv', 'page', 'normal');
}
}
}
add_action( 'admin_menu', 'remove_page_attribute_meta_box' );
Paste this in your theme's functions.php file.

How can i limit a user in wordpress

Can any one please tell me how can i limit a user to edit only his post.I used the role editor plugin but it allow the user to edit all users post.I'm creating a classified site plugin where a user can post(custom post type)and he can edit his post.
You can user advanced access manager plugin for same.
You can limit a user to only edit their own posts using this bit of code.
function my_authored_content($query) {
//get current user info to see if they are allowed to access ANY posts and pages
$current_user = wp_get_current_user();
// set current user to $is_user
$is_user = $current_user->user_login;
//if is admin or 'is_user' does not equal #username
if (!current_user_can('manage_options')){
//if in the admin panel
if($query->is_admin) {
global $user_ID;
$query->set('author', $user_ID);
}
return $query;
}
return $query;
}
add_filter('pre_get_posts', 'my_authored_content');
function remove_menu_items() {
$current_user = wp_get_current_user();
if ( !current_user_can( 'manage_options' ) ) {
//hides comments menu
remove_menu_page( 'edit-comments.php' );
// hides posts menu
remove_menu_page( 'edit.php' );
hides pages menu
remove_menu_page( 'edit.php?post_type=page' );
}
}
add_action( 'admin_menu', 'remove_menu_items' );
Hope this helps you :-)

Resources