Make a single page in WordPress available only for Admin and Subscribers

please try this if ( have_posts() ) : while ( have_posts() ) : the_post(); if ( get_post_status (get_the_id()) == ‘private’ ) { if ( current_user_can( ‘administrator’ ) ) { the_title(); the_content(); } else if ( current_user_can( ‘subscriber’ ) ) { the_title(); the_content(); } else { echo ‘this post is private’; } } else { the_title(); … Read more

allowing subscriber role to delete their own posts

The capability required to delete posts is delete_posts. If you want them to be able to delete their own published posts, the capability is delete_published_posts. The capability required to view the administration panel is read. Subscribers have this capability natively, so unless you have removed it, subscribers can access the backend. I would write a … Read more

Problems using ‘add_role’

You can pass the user’s role directly to wp_insert_user(). First, you can try to add your custom role to the WP_Roles object, then pass the custom role to the user properties: // Add custom roles only once to avoid unnecessary database calls. // It is usually done during plugin activation. register_activation_hook( __FILE__, ‘cyb_add_roles’ ); function … Read more

Allow user edit widgets

The edit_theme_options capability controls access to the widgets page but also to the menus page. You can then remove the menus submenu from appearance for a specific role, and if someone tries to get there by url, redirect it : /** * Remove the “Menus” submenu from Appearance */ function remove_menus() { if (in_array(‘administrator’, wp_get_current_user()->roles)) … Read more

How to restrict an admin page, if the user is not superadmin?

To remove from the admin menu, you could use remove_menu_page(): add_action( ‘admin_menu’, function ( ) { if (is_super_admin()) return; remove_menu_page(); // or remove_submenu_page( … },99); If for some reason the page still exists, it’s just missing it’s menu link, you could check the get_current_screen() to see if the page is being viewed, and prevent access: … Read more

Change user role of a particular user at specific time

You can do something like this add_action(“after_switch_theme”, “schedule_cron_job”); // hook the schedule on theme switch or plugin activation based on the your usage also switch your theme after putting this on functions.php function schedule_cron_job(){ if (! wp_next_scheduled ( ‘import_into_db’ )) { wp_schedule_event(strtotime(’12:04:00′), ‘daily’, ‘import_into_db’); } } add_action(‘import_into_db’, ‘your_cron_job’); // You were hooking to wp function … Read more

Block user roles from accessing the WordPress dashboard

You can make an array of the roles that need to be blocked, and use array_intersect() to check if the current user is in any of those roles. function wpse66094_no_admin_access() { $redirect = isset( $_SERVER[‘HTTP_REFERER’] ) ? $_SERVER[‘HTTP_REFERER’] : home_url( “https://wordpress.stackexchange.com/” ); global $current_user; $blocked_roles = array( ‘shopkeeper’, ‘block-this-role’, ‘block-this-role-too’ ); $user_roles = $current_user->roles; // … Read more