Remove dashbord acess form wordpress admin - wordpress

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

Related

Add custom content after redirect wordpress

I've created a function redirecting all my logged-out users to the membership page.
I want them to buy a package plan before they can view the page they tried to visit before being redirected.
If logged-in -> view the download page.
If logged-out -> redirect to the membership page -> purchase a package -> view the download page.
I want to add content to the membership page, something like:
'The product that you are trying to download cannot be purchased individually, but it's part of our membership package'
The problem is that I couldn't find a way to execute any function after the wp_redirect() function. So I don't know how to display the content only to those redirected.
Is there a way to achieve it?
in your header.php:
if( !is_user_logged_in() ){
nocache_headers();
wp_safe_redirect( get_permalink( 'YOUR_MEMBERSHIP_PAGE_ID' ) . '?message=true' );
exit;
}
in your functions.php:
function add_query_vars_filter( $vars ){
$vars[] = "message";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
in your membership page:
if( get_query_var('message') && !is_user_logged_in() ){
echo 'The product that you are trying to download cannot be purchased individually, but it\'s part of our membership package';
}

Disable Elementor on specific page

I have a plugin that uses the default woocommerce my account shortcode and displays the page only for a specific role. But, I have customized the My Account page using elementor & crocoblock, and the plugin is not working properly. They did give a workaround for this in the documentation, but I can't seem to make it work. This is the code
add_filter( 'elementor/frontend/the_content', 'wcb2bsa_disable_elementor_on_sales_agents_area' );
function wcb2bsa_disable_elementor_on_sales_agents_area( $content ) {
if ( is_account_page() && is_user_logged_in() && wcb2bsa_has_role( get_current_user_id(), 'sales_agent' ) ) {
return '[woocommerce_my_account]';
}
return $content;
}
Is it because the elementor/frontend/the_content part is changed?

How to secure an external page that is included in a menu page of WordPress?

I have created a theme options page for my WordPress theme with the following code in functions.php file:
add_action( 'admin_menu', 'register_options_page' );
function register_options_page()
{
$my_hook = add_menu_page( 'Theme Options', 'Theme Options', 'manage_options', 'merry_options', 'get_theme_options', 'dashicons-share-alt', 99 );
// var_dump($my_hook); die();
}
function get_theme_options()
{
include_once get_template_directory()."/framework/themeoptions.php";
}
As you can see, it includes an external page located at .../framework/themeoptions.php. Now the problem is anyone can go and open that URL to access this page directly. I want that page only to accessed when the WordPress admin is signed in. Can you tell me the right way to do it?
I would suggest the following function:
if ( is_admin() ) {
echo "You are viewing the WordPress Administration Panels";
} else {
echo "You are viewing the theme";
}
as the right way to check for administrator privileges.
You can read more about this function in the Wordpress Codex: http://codex.wordpress.org/Function_Reference/is_admin
Figured out a way. I can use
<?php if (current_user_can('administrator')) { }?>
to see if WordPress admin is accessing the page. Is there a better approach?

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.

Disable the admin bar for all users except admin

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

Resources