WordPress Remove Menu Pages Except for Super Admin - php

I have written a snippet to remove some WordPress menu pages.
add_action( 'admin_menu', 'notadmin_remove_menus', 999 );
function notadmin_remove_menus() {
remove_menu_page('edit.php');
remove_menu_page('edit.php?post_type=page');
remove_menu_page('upload.php');
remove_menu_page('jetpack');
remove_menu_page('users.php');
remove_menu_page('tools.php');
remove_menu_page('edit-comments.php');
remove_menu_page('edit.php?post_type=featured_item');
remove_menu_page('edit.php?post_type=feedback');
remove_menu_page('edit.php?post_type=blocks');
}
The problem is this code also remove menu pages for super admin. I want to remove these menu for all users except super admin. Please help.

Try this code
add_action( 'admin_menu', 'notadmin_remove_menus', 999 );
function notadmin_remove_menus() {
if ( !is_super_admin() ) {
remove_menu_page('edit.php');
remove_menu_page('edit.php?post_type=page');
remove_menu_page('upload.php');
remove_menu_page('jetpack');
remove_menu_page('users.php');
remove_menu_page('tools.php');
remove_menu_page('edit-comments.php');
remove_menu_page('edit.php?post_type=featured_item');
remove_menu_page('edit.php?post_type=feedback');
remove_menu_page('edit.php?post_type=blocks');
}
}
or
if ( !is_super_admin() ) {
add_action( 'admin_menu', 'notadmin_remove_menus', 999 );
}
function notadmin_remove_menus() {
remove_menu_page('edit.php');
remove_menu_page('edit.php?post_type=page');
remove_menu_page('upload.php');
remove_menu_page('jetpack');
remove_menu_page('users.php');
remove_menu_page('tools.php');
remove_menu_page('edit-comments.php');
remove_menu_page('edit.php?post_type=featured_item');
remove_menu_page('edit.php?post_type=feedback');
remove_menu_page('edit.php?post_type=blocks');
}

Related

How to rename a menu tab under WooCommerce tab on WordPress admin dashboard

I need help in renaming a tab menu item under woocommerce tab on wordpress admin. We installed a plugin that appears as a submenu on woocommerce tab. Can anyone please help me on this?
I found this code below to rename a tab menu, but I dont know what is the tabmenu key of it. Or anyone here how to check tab menu key on my current tab menu items?
add_action( 'admin_menu', 'custom_change_admin_label', 99);
function custom_change_admin_label() {
global $menu;
//global $submenu;
$menu[5][0] = 'Articles';
}
Thank you
The first part of the code is to debug, this will show you the menu in detail on the dashboard. (you can remove this afterwards)
The 2nd part in this example changes the 'coupons' label to 'voucher'
It is therefore a matter of adjusting based on the detail
// DEBUG: This displays the complete wordpress admin menu on your dashboard for admin only.
function debug_admin_menus() {
global $menu, $submenu, $pagenow;
if ( current_user_can('manage_options') ) {
if( $pagenow == 'index.php' ) { // print on dashboard
echo '<pre>', print_r( $menu, 1 ), '</pre>'; // top level menus
echo '<pre>', print_r( $submenu, 1 ), '</pre>'; // submenus
}
}
}
add_action( 'admin_notices', 'debug_admin_menus' );
// Change label, in this example changes the 'coupons' label to 'voucher'
function custom_change_admin_label() {
global $menu, $submenu;
$submenu['woocommerce'][2][0] = 'Voucher'; // rename 'coupons' label
}
add_action( 'admin_menu', 'custom_change_admin_label' );

Hiding widgets for specific user roles

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

Hide 'add to cart' button ONLY on woocommerce shop/category pages

I want to hide the button on my shop pages, but I would like to show it on other posts and pages.
I've found this code to hide the add to cart button on my whole website:
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
How can I tweak it, so it only removes the button on woocommerce shop and catagory pages?
You can use the Woocommerce conditional tags to check:
http://docs.woothemes.com/document/conditional-tags/
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
if( is_product_category() || is_shop()) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
}
This could also be done with CSS by targeting the relevant classes:
.cart{display:none;}
.avia_cart_buttons{display:none;}
In my case there is that avia because i use Enfold Theme. With inspect element find out your class where the buton is located. and declare it invisible.
Another example is:
.woocommerce .products .shop-column.product-hover-style-2 .product-content
.product-add-to-cart-btn{
display:none !important;
}
add_action('wp','only_add_bierkoerier_in_cart', 'woocommerce_before_cart');
function only_add_bierkoerier_in_cart() {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$bierkoerier_in_cart = false;
$categories = get_categories();
if ( has_term( 'bierkoerier', 'product_cat', $cart_item['product_id'] ) ) {
$bierkoerier_in_cart = true;
break;
}
}
if($bierkoerier_in_cart) {
wc_print_notice( 'omdat u bierkoerier producten in uw winkelwagen heeft
kunt u geen winkelitems toevoegen', 'notice' );
if(is_shop() || is_product() || is_product_category()) {
remove_action( 'woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
}
To remove the "Add to cart" button
You need to use hook which not affect other code-
add_action( 'woocommerce_after_shop_loop_item', 'remove_loop_button', 1 );
function remove_loop_button()
{
if( is_product_category() || is_shop()) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
}
}
this will remove add to cart button from shop/category pages .
Here you can get WooCommerce Action and Filter Hook
-https://docs.woothemes.com/wc-apidocs/hook-docs.html
Here is a plugin you can use to remove hide disable add to cart button https://wordpress.org/plugins/woo-options/
That's quite simple as i have gone through several tutorials when i was trying to fix it . You have to just put this code in woocommerce.php to hide add to cart button for shop page.
function WpBlog() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
return WooCommerce::instance();
}
Hope that would work for you, if not let me know i will guide you
To remove add to cart buttons from shop, product category, and single product pages, use below steps:
Locate the functions.php in child theme. Child theme prevents changes been overwritten by WP updates.
https://www.dreamhost.com/wordpress/create-woocommerce-child-theme/
Put below code in functions.php:
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
if( is_product_category() || is_shop()) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
}
add_action( 'woocommerce_single_product_summary', 'woocommerce_before_cart' );
function woocommerce_before_cart() {
if( is_product()) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30);
}
}

remove_menu_page() not getting initialized within if condition

I am trying to remove some menu pages based on user role, but when I add the function inside the if condition it doesn't do anything.
function contributor_posts_action() {
if ($role == 'contributor_posts') { // contributor_posts - custom role
// echo 'here'; for testing purposes and WORKS, so it goes under the if condition
add_action( 'admin_menu', 'remove_menus_contrib' );
function remove_menus_contrib(){
remove_menu_page( 'edit-comments.php' );
remove_menu_page( 'tools.php' );
remove_menu_page( 'edit.php?post_type=directory' );
remove_menu_page( 'edit.php?post_type=city' );
} // this function doesn't get hooked
add_action( 'admin_bar_menu', 'remove_admin_bar_items', 999 );
function remove_admin_bar_items( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'new-directory' );
$wp_admin_bar->remove_node( 'new-city' );
}// this one works properly. It's for removing for admin bar.
}
}
add_action( 'admin_init', 'contributor_posts_action' );
Try pulling the remove_menus_contrib() and the add_action( 'admin_menu', 'remove_menus_contrib' ) hook function out of your contributor_posts_action() function.
Some Wordpress hooks won't work inside other (custom) functions.

showing add to cart for logged in users only..woocommerce

been using this code to hide prices..
add_filter('woocommerce_get_price_html','members_only_price');
function members_only_price($price){
if(is_user_logged_in() ){
return $price;
}
else return 'Login or Register to see price!';
}
tried modifying it to use for hiding add to cart as well..but no avail..
anyone?
Extending the above code (thanks Ewout), the following code will get rid of all prices and 'add to cart' buttons on all woocommerce products, as well as provide an explanation as to why. I needed the code for a website that offers direct selling products and to comply with their rules, I cannot show prices to the general public.
Add the filter to your theme's functions.php file.
add_filter('woocommerce_get_price_html','members_only_price');
function members_only_price($price){
if(is_user_logged_in() ){
return $price;
}
else {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
return 'Only Registered Users are able to view pricing.';
}
}
Have you tried something like this? You would set woocommerce to only show prices when user is logged in.
add_filter('catalog_visibility_alternate_price_html', 'my_alternate_price_text', 10, 1);
function my_alternate_price_text($content) {
return 'Login or Register to see price!';
}
Reference: http://docs.woothemes.com/document/catalog-visibility-options/
EDIT:
The reference material has the cart visibility reference
add_filter('catalog_visibility_alternate_add_to_cart_button', 'my_alternate_button', 10, 1);
function my_alternate_button($content) {
return 'Login or Register to see cart!';
}
What about CSS?
button.add-to-cart {
display: none;
}
body.logged-in button.add-to-cart {
display: block;
}
We can do this easily through the woocommerce_is_purchasable and woocommerce_get_price_html hooks.
Code:
// Disable purchase for non-logged-in users. (Remove add-to-cart button).
function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
if ( ! is_user_logged_in() ) {
return false;
}
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );
// Show "Login to see prices" instead of price for non-logged in users.
function m3wc_woocommerce_get_price_html( $price ) {
if ( ! is_user_logged_in() ) {
return 'Login to see prices';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'm3wc_woocommerce_get_price_html', 10, 2 );
And the result:
Source: WooCommerce - Disable purchase for non-logged-in users. (Remove add-to-cart button).

Categories