I am trying to allow the custom role bbp_moderator to view the Users Section from wordpress backend, but for some reason is not working. i can see the roles via the $wp_roles global variable and the capability edit_users had been added the the bbp_moderator role.
function add_capablilities_to_mod() {
$role = get_role('bbp_moderator');
$role->add_cap('edit_users');
}
function add_user_table_to_moderator() {
global $wp_roles;
//
//var_dump(current_user_can( 'bbp_moderator' )); -> true
if ( current_user_can( 'bbp_moderator' ) ) {
add_action( 'plugins_loaded', 'add_capablilities_to_mod' );
}
}
add_action('init', 'add_user_table_to_moderator');
Related
Following the woocommerce documentation, I added an endpoit to my-account page in woocommerce.
I want to make this endpoint visible only to a specific user role, lets say shop_manager.
Is there a way to redirect to a 404 page users who try to access directly that endpoint?
Thanks in advance.
Assuming that you have already created a custom endpoint to my account section (see this related answer), you can redirect all non allowed user roles to a specific page using template_redirect hook in this simple way:
add_action( 'template_redirect', 'custom_endpoint_redirection' );
function custom_endpoint_redirection() {
$allowed_user_role = 'administrator';
$custom_endpoint = 'my-custom-endpoint';
if ( is_wc_endpoint_url($custom_endpoint) && ! current_user_can($allowed_user_role) ) {
$redirection_url = home_url( '/404/' );
wp_redirect( $redirection_url );
exit;
}
}
You need to specify your custom end point, your allowed user role and the url redirection.
Code goes in functions.php file of your active child theme (or active theme). It could works.
Related:
WooCommerce - Assign endpoints to multiple custom templates in my-account page
WooCommerce: Assigning an endpoint to a custom template in my account pages
WooCommerce: Adding custom template to customer account pages
Custom my account new menu item for a specific user role in Woocommerce
Just add the follows code snippet in your active theme's functions.php and this is only for administrator user role, you can change it as per you -
function add_custom_my_account_endpoint() {
add_rewrite_endpoint( 'shop_manager', EP_PAGES );
}
add_action( 'init', 'add_custom_my_account_endpoint' );
function add_custom_wc_menu_items( $items ) {
$user = wp_get_current_user();
if( $user && in_array( 'administrator', $user->roles ) ){
$items[ 'shop_manager' ] = __( 'Shop Manager', 'text-domain' );
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'add_custom_wc_menu_items' );
function add_shop_manager_endpoint_content(){
$user = wp_get_current_user();
if( $user && !in_array( 'administrator', $user->roles ) ) return;
echo "Your content goes here";
}
add_action( 'woocommerce_account_shop_manager_endpoint', 'add_shop_manager_endpoint_content' );
After this just flush_rewrite_rules from Backend Settings > Permalinks. Thats it.
My Image
I just wanted to hide specific page for certain users.
function remove_menus(){
// get current login user's role
$roles = wp_get_current_user()->roles;
// test role
if( in_array('administrator',$roles)){
remove_menu_page( 'edit-comments.php' ); //Posts
remove_menu_page( 'tools.php' );
remove_menu_page('edit.php');
remove_menu_page('wpcf7');
}
}
add_action( 'admin_menu', 'remove_menus' , 100 );
This what I am tried upto now and its working fine for all the page.
My question is I dont want to show home - Front page (Please see my image) If logged in user is not admin. and also I want to hide add new
You can use user's role capabilities and allow on the basis of role for add new items.
function manage_user_action() {
// get current login user's role
$roles = wp_get_current_user()->roles;
if( !in_array('administrator',$roles)){
//remove capabilities
$roles->remove_cap( 'edit_pages');
}
}
add_action( 'admin_init', 'manage_user_action');
To Remove Page from list
function jp_exclude_pages_from_admin($query) {
global $pagenow, $post_type;
if ( !current_user_can( 'administrator' ) && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__not_in'] = array( '10'); // Enter your page IDs here
//don't forget to the query
return $query;
}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' );
For more help see this link : Click Here
I have extended #dineshkashera answer to target a specific user by ID and the code should be as follows :
function jp_exclude_pages_from_admin($query) {
global $pagenow, $post_type;
$current_user_id = get_current_user_id();
if ( $current_user_id == 2 && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__not_in'] = array( '11', '12','13' ); // Enter your page IDs here
}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' );
This code will get the user with ID of 2 and remove the pages IDs 11,12 and 13 from their WP page edit screen.
So its about hiding widgets for specific user roles excluding Admin.Using custom sidebar plugin i don't want to be display on users end.Listed all Dashboard widgets through
function list_active_dashboard_widgets() {
global $wp_meta_boxes;
foreach (array_keys($wp_meta_boxes['dashboard']['normal']['core']) as $name) {
echo '<div>' . $name . '</div>';
}
}
add_action('wp_dashboard_setup', 'list_active_dashboard_widgets');
and found customsidebars-mb
What i am doing without succes is adding this code to hide sidebar options widget from users panel keeping it on admin panel.
function disable_default_dashboard_widgets() {remove_meta_box('customsidebars-mb', 'dashboard', 'normal');
}
add_action('admin_menu', 'disable_default_dashboard_widgets');
if (!current_user_can('manage_options')) {
add_action('wp_dashboard_setup', 'disable_default_dashboard_widgets');
}
**
Store front Theme Woocommerce Plugin
**Will be phasing out all plugins with codes
You can try this one in your functions.php,
add_action( 'widgets_init', 'remove_widgets_wpse_89138' , 15 );
function remove_widgets_wpse_89138()
{
// http://codex.wordpress.org/Function_Reference/is_admin
if( !is_admin() )
return;
// Grab current user info
global $current_user;
// Check for specific user
/*
$username = $current_user->user_login;
if( 'the_user_login' != $username)
return;
*/
// Check for capability
if( current_user_can( 'add_users' ) )
return;
unregister_widget( 'WP_Widget_Pages' );
}
Hope this will helps you. For more please visit, URL 1, URL 2
Hello friends,
i need little help in Wordpress. I am trying to hide the authorbox that appears under the post for specific user only.
Example if post i am looking into is posted by admin, then i want to hide the authorbox under post content that is posted by admin, but should show for all other users ? i tried different functions but not able to success on this.
I want to completly hide the authorbox, i know it can be done with css code that is
.author-box { display:none;}
but this hides the overall authorbox of complete them, i just want to hide the authorbox on the posts that are made by admin ?
i am using genesis framework, so please suggest if any help you can make here.
Thanks
In your theme's functions.php file, add something like this:
function remove_my_post_metabox() {
global $post;
// If the post author ID is 1, then remove the meta box
// You would need to find the ID of the author, then put it in place of the 1
if ( $post->post_author == 1) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );
If you need to find out the role of the author, and truly do it based on the role (admin, for example), then it would be more like this:
function remove_my_post_metabox() {
global $post;
// If there's no author for the post, get out!
if ( ! $post->post_author) {
return;
}
// Load the user
$user = new WP_User( $post->post_author );
// Set "admin" flag
$is_admin = FALSE;
// Check the user roles
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role ) {
if ( $role == 'administrator' ) {
$is_admin = TRUE;
}
}
}
// Lastly, if they ARE an admin, remove the metabox
if ( $is_admin ) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );
I want to add a capability to one of the default roles in WordPress. The add_cap article advises people to do this sort of thing on theme activation because the setting is saved to the database:
NB: This setting is saved to the database, so it might be better to run this on theme/plugin activation
The function I intend to use is:
function add_theme_caps() {
$role = get_role( 'author' );
$role->add_cap( 'edit_others_posts' );
}
add_action( 'admin_init', 'add_theme_caps');
As you can see, I'm currently hooking to admin_init, which causes the function to be run each time the admin area is accessed. How can I run the function on theme activation only?
You can use after_switch_theme . It will affect only your theme, of course.
http://codex.wordpress.org/Plugin_API/Action_Reference/after_switch_theme
I feel you should try something like this
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {
$role = get_role( 'author' );
$role->add_cap( 'edit_others_posts' );
}