I would like to hide a specific woocommerce setting tab by user role. Not the entire submenu, but just a tab(checkout to be specific).
I want shop managers to be able to access most of the settings, but be unable to affect the checkout settings.
How can I achieve this?
If you want to remove the tabs instead of hiding them using CSS, then you can add the following to yours theme functions.php:
add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $tabs ) {
// Declare the tabs we want to hide
$tabs_to_hide = array(
'Tax',
'Checkout',
'Emails',
'API',
'Accounts',
);
// Get the current user
$user = wp_get_current_user();
// Check if user is a shop-manager
if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
// Remove the tabs we want to hide
$tabs = array_diff($tabs, $tabs_to_hide);
}
return $tabs;
}
This uses the WooCommerce 'woocommerce_settings_tabs_array' filter. For more information on all the WooCommerce filters and hooks you can look here: https://docs.woocommerce.com/wc-apidocs/hook-docs.html
This just has the added benefit that it is no longer in the HTML, so if anyone looks at the source, they won't find the elements.
You can still access the URLs. This is just a way of removing the tabs instead of hiding them.
EDIT:
I've figured out how to stop access to the URLs. Copy the following:
add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $array ) {
// Declare the tabs we want to hide
$tabs_to_hide = array(
'tax' => 'Tax',
'checkout' => 'Checkout',
'email' => 'Emails',
'api' => 'API',
'account' => 'Accounts',
);
// Get the current user
$user = wp_get_current_user();
// Check if user is a shop_manager
if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
// Remove the tabs we want to hide from the array
$array = array_diff_key($array, $tabs_to_hide);
// Loop through the tabs we want to remove and hook into their settings action
foreach($tabs_to_hide as $tabs => $tab_title) {
add_action( 'woocommerce_settings_' . $tabs , 'redirect_from_tab_page');
}
}
return $array;
}
function redirect_from_tab_page() {
// Get the Admin URL and then redirect to it
$admin_url = get_admin_url();
wp_redirect($admin_url);
exit;
}
This is pretty much the same as the first bit of code, apart from the array is structured differently and I've added a foreach. The foreach goes through the list of tabs we want to block, hooks into the 'woocommerce_settings_{$tab}' action which is used to show the settings pages.
Then I created a redirect_from_tab_page function to redirect the users to the default admin URL. This stops direct access to the different settings tabs.
Put this code in your theme/child theme functions.php or somewhere else:
if (!function_exists('hide_setting_checkout_for_shop_manager')){
function hide_setting_checkout_for_shop_manager() {
$user = wp_get_current_user();
//check if user is shop_manager
if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
echo '<style> .woocommerce_page_wc-settings form .woo-nav-tab-wrapper a[href="'.admin_url('admin.php?page=wc-settings&tab=checkout').'"]{ display: none; } </style>';
}
}
}
add_action('admin_head', 'hide_setting_checkout_for_shop_manager');
The style will be output to html head only at wp-admin and the login user role is shop_manager.
For more about admin_head hook, please check https://codex.wordpress.org/Plugin_API/Action_Reference/admin_head
Related
I have this is WordPress Post editing Url:
https://example.com/wp-admin/post.php?post=ID&action=edit
and I want to change it to Slug Not the ID Like This:
https://example.com/wp-admin/post.php?post=Slug&action=edit
I was trying to edit the post with this Url but is not working:
https://example.com/wp-admin/post.php?post=MyPostSlug&action=edit
In order to change the edit post link structure, you can use the get_edit_post_link filter like so:
add_filter( 'get_edit_post_link', 'so_73914075_get_edit_post_link', 10, 3);
function so_73914075_get_edit_post_link($link, $post_id, $context) {
$post = get_post( $post_id );
if ( ! in_array( $post->post_type, array( 'post', 'page' ) ) ) {
return $link;
}
$post_type_object = get_post_type_object( $post->post_type );
if ( 'revision' === $post->post_type ) {
$action = '';
} elseif ( 'display' === $context ) {
$action = '&action=edit';
} else {
$action = '&action=edit';
}
if ( 'display' === $context ) {
$post_type = '&post-type=' . $post->post_type;
} else {
$post_type = '&post-type=' . $post->post_type;
}
$custom_edit_link = str_replace( '?post=%d', '?post-name=%s', $post_type_object->_edit_link );
return admin_url( sprintf( $custom_edit_link . $action . $post_type, $post->post_name ) );
}
This will change the edit links for regular posts and pages to something like this:
https://example.com/wp-admin/post.php?post-name=the-post-slug&action=edit&post-type=post
WARNING: make sure you limit this to only the post types you need to change the URL for. Making this change global will almost surely
have unintended effects over other plugins
However, making the admin panel actually load the edit screen is not that easy.
Looking at the source code of the wp-admin/post.php file, you will see that there is no way to hook into the flow and populate the $post_id variable with the post id matching the slug you are sending through.
That means you have 2 options:
RECOMMENDED Update the edit link to whatever format you want and then create an admin redirect function that pulls the slug from the initial URL and redirects to the actual edit page with the ?post=%d in it.
NOT RECOMMENDED Create a new admin edit page that will understand your URL structure and include the wp-admin/post.php after the code that pulls the $post_id based on the slug.
The 2nd method might come with lots of extra code you need to write to cover all the cases and all the instances a post reaches the post.php in the admin panel
Hi I'm using this code to make only the admin see all the posts and editor users can see only their own added posts, but it is also restricts custom feilds form showing when I add or edit any post using an editor user (The fields inside the post page), how can I exclude the custom fields so all the users including editors will be able to see them on each post page.
add_shortcode('pledge_count', 'get_pledge_count');
add_filter( 'pre_get_posts', 'fb_pre_get_posts' );
function fb_pre_get_posts( $queryobj ) {
if(is_admin()){
if ( ! current_user_can( 'edit_users' ) )
$queryobj->query_vars[ 'author' ] = get_current_user_id();
}
return $queryobj;
}
I solved the issue and posting it in case someone else facing this.
This is solved by specifying the post types that I'm going to give the access to the user created it:
add_shortcode('pledge_count', 'get_pledge_count');
add_filter( 'pre_get_posts', 'fb_pre_get_posts' );
function fb_pre_get_posts( $queryobj ) {
switch($query->query_vars['post_type']){
case 'attachment': // Media library
$queryobj->query_vars[ 'author' ] = get_current_user_id();
break;
case 'post': // Posts
$queryobj->query_vars[ 'author' ] = get_current_user_id();
break;
case 'page': // Pages
$queryobj->query_vars[ 'author' ] = get_current_user_id();
break;
} // switch post_type
return $queryobj;
}
I made a custom navigation page for users, using the plugins.php file.
But I would like this page/option to be only available for some types of users; and make it the default landing page for them.
I can't figure out how to that.
I tried to make it the default landing page, and in the plugins template file, adding a condition that redirect users depending on their type... but redirection doesn't work by there it seems.
Any clue, plz?
I'm using Wordpress 5.8.2 and Buddypress 9.1.1.
Thanks
I found a solution.
First, here's how I created a new navigation item; I put this code in my bp-custom.php file:
function bp_custom_user_nav_item() {
global $bp;
$args = array(
'name' => __('newnavitem', 'buddypress'),
'slug' => 'newnavitem',
'default_subnav_slug' => 'newnavitem',
'position' => 0,
'show_for_displayed_user' => true,
'screen_function' => 'bp_custom_user_nav_item_screen',
'item_css_id' => 'newnavitem'
);
bp_core_new_nav_item( $args );
}
add_action( 'bp_setup_nav', 'bp_custom_user_nav_item', 99 );
After I created a new directory in community/members/single in my theme directory, and in this directory I will put my newnavitem loop and edit template files.
And I linked the newnavitem and its template with this function
function bp_custom_user_nav_item_screen() {
add_action( 'bp_template_content', 'bp_custom_screen_content' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/newnavitem' ) );
}
And here's how I disabled the current nav item for certain user types, and set it as default landing page for another certain user types.
function conditionally_disable_newnavitem( $enabled, $component) {
if (user_can(bp_displayed_user_id(),'firstusertype') && $component === 'newnavitem') {
$enabled = false;
}
return $enabled;
}
add_filter( 'bp_is_active', 'conditionally_disable_new_navitem', 10, 2 );
function set_default_component () {
if ( user_can(bp_displayed_user_id(),'secondusertype') || user_can(bp_displayed_user_id(),'thirdusertype')) {
define ( 'BP_DEFAULT_COMPONENT', 'newnavitem' );
add_filter( 'bp_is_active', function($retval, $component){
if($component === 'newnavitem') return true;
return $retval;
}, 10, 2 );
} else {
define ( 'BP_DEFAULT_COMPONENT', 'activity' );
}
}
add_action( 'bp_core_setup_globals', 'set_default_component' );
We have a Wordpress installation with a lot of users. If we wanted to delete a user so far, it took a long time until the delete button appeared. Then the deletion always worked. But now the button doesn't appear anymore. I have the feeling it's because it always first goes through the 400,000 users and it's because of that. I'm using WP version 4.9.8
if there are many users it is loading very long. then you can filter the users to be displayed like this:
add_action( 'load-users.php', function () {
// Make sure the "action" is "delete".
if ( 'delete' !== filter_input( INPUT_GET, 'action' ) ) {
return;
}
add_filter( 'wp_dropdown_users_args', function ( $query_args, $args ) {
if ( 'reassign_user' === $args[ 'name' ] ) {
$query_args[ 'role' ] = 'administrator';
}
return $query_args;
}, 10, 2 );
} );
My wordpress admin bar 'My Account' links takes the user to the back end to manage their profile etc. There are also buddypress links on that menu which take the user to manager their profile on the front end. I like the avatar with the 'Howdy' but I don't want users on the back
end; so I need to change the WP links to take them to the front end.
I found how to remove links and add links to the admin bar 'My Account' but I just want to change the url of those top links from /wp-admin/profile to /members/user/profile.
admin-bar.php
/**
* Add the "My Account" item.
*
* #since 3.3.0
*
* #param WP_Admin_Bar $wp_admin_bar
*/
if ( current_user_can( 'read' ) ) {
$profile_url = get_edit_profile_url( $user_id );
Would changing the 'get_edit_profile_url' from a wordpress admin url to the buddypress members url be the simplest way to accomplish my goal? ... and How would I do that to test it?
Thanks to anybody who can help me, I'm not very good with php.
I couldn't find any help with changing the url however this redirect in my functions file keeps the user on the front end.
Thanks to: http://blog.happyplugins.com/redirect-user-profile-page-frontend-page/
add_action ('init' , 'prevent_profile_access');
function prevent_profile_access() {
if (current_user_can('manage_options')) return '';
if (strpos ($_SERVER ['REQUEST_URI'] , 'wp-admin/profile.php' )){
wp_redirect ('/members/' .bp_core_get_username(bp_loggedin_user_id() ));
die();
}
}
I'd still like to know how to change the URL instead of using the redirect, if anyone knows how!
bp_core_get_userlink( $user_id )
add_action('wp_before_admin_bar_render', 'rew_admin_bar_remove_wp_profile', 0);
function rew_admin_bar_remove_wp_profile() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('edit-profile');
}
add_action('admin_bar_menu', 'rew_add_bbp_profile', 999);
function rew_add_bbp_profile($wp_admin_bar) {
$current_user = wp_get_current_user() ;
$user=$current_user->user_nicename ;
$user_slug = get_option( '_bbp_user_slug' ) ;
if (get_option( '_bbp_include_root' ) == true ) {
$forum_slug = get_option( '_bbp_root_slug' ) ;
$slug = $forum_slug.'/'.$user_slug.'/' ;
}
else {
$slug=$user_slug . '/' ;
}
$profilelink = '/' .$slug. $user . '/edit' ;
$wp_admin_bar->add_node( array(
'parent' => 'user-actions',
'id' => 'bbp-edit-profile',
'title' => 'Edit Profile',
'href' => $profilelink,
) );
}
get you to the bbpress user profile