Hide/Show MULTIPLE Custom tab only to specific role?
-
Hello, i saw this post and i’d like some help with it. I managed to get everything working, however, i have 2 additional tabs, i’m not an expert with php. How would i go about adding more tabs?
Thank you.
-
Hi @davidcova,
Thank you for finding that support ticket for me – when I saw the subject of your support ticket I remembered that one and was going to go find it.
The first thing we need to know is the slug of your additional tabs. If your tab is called “Description” the slug will be
description; likewise if it’s called Additional Information the slug will beadditional_information. For the purposes of this example, let’s call your tabs “Additional Tab 1” and “Additional Tab 2.” Therefore the slugs we’re working with areadditional_tab_1andadditional_tab_2. If you need any help finding the slug of your tab (e.g. if it includes special characters), just send me a link to your product page and I will tell you the slug names.The second thing we need to know is the role that you want to check against. For our purposes, we’ll use the administrator role.
The function should then look like this:
add_filter( 'yikes_woo_filter_all_product_tabs', 'yikes_woo_add_tab_permissions', 10, 2 ); function yikes_woo_add_tab_permissions( $tabs, $product ) { if ( isset( $tabs['additional_tab_1'] ) && ! current_user_can( 'administrator' ) ) { unset( $tabs['additional_tab_1'] ); } if ( isset( $tabs['additional_tab_2'] ) && ! current_user_can( 'administrator' ) ) { unset( $tabs['additional_tab_2'] ); } return $tabs; }Make sure to replace your current function with the one above (rather than having both of them).
Let me know how that goes.
Cheers,
Kevin.Thank you so much for such a fast response, you fixed my issue 🙂
Just so i don’t have to bother you again in the future, if i wanted to add permission to the administrator but also to an editor, how could i do that?
Hey no problem.
Do you mean you want to allow administrators and editors to see the tabs?
That would look like:
add_filter( 'yikes_woo_filter_all_product_tabs', 'yikes_woo_add_tab_permissions', 10, 2 ); function yikes_woo_add_tab_permissions( $tabs, $product ) { if ( isset( $tabs['additional_tab_1'] ) && ( ! current_user_can( 'administrator' ) || ! current_user_can( 'editor' ) ) ) { unset( $tabs['additional_tab_1'] ); } if ( isset( $tabs['additional_tab_2'] ) && ( ! current_user_can( 'administrator' ) || ! current_user_can( 'editor' ) ) ) { unset( $tabs['additional_tab_2'] ); } return $tabs; }That’s it, perfect!
Thank you so much <3
Do you offer any support for printing the page with all created tabs contents displaying btw? 🙂
You’re welcome 🙂
What do you mean by printing the page? Are you using a plugin that generates PDFs or print-friendly versions of your product pages?
Yes, i’m using WooCommerce ProductPrint to generate a printable page, however, the generated tabs won’t show upon printing, not sure if there’s a way to work with this or not.
—wrong reply—
-
This reply was modified 8 years, 8 months ago by
yikesitskevin.
If there is a filter available we could hook into it to add the tabs but I don’t have any familiarity with that plugin. Could you ask the developer (or look through the documentation) to see if there is a way to add product meta data to the printable page?
If you can find me a filter I can write you a function!
Sadly the two plugins i found that could do what i intend are long discontinued, WooCommerce PDF & Print and WooCommerce ProductPrint haven’t been updated in over 2 years. And from what i could see, there’s no much information about filters on those.
I’m fairly new to WordPress though, perhaps if you have a suggestion, i’d gladly look into it. I only want to print the content of Woocommerce products. But only for the restrictions you added earlier.
I have worked with folks who have used premium plugins that do this sort of thing but I have no real experience with the actual plugins so I cannot recommend them. Sorry about that.
I tried the code you gave for multiple viewers but the tabs disappeared for both admin and editor now :/
Oops my mistake. This should work:
add_filter( 'yikes_woo_filter_all_product_tabs', 'yikes_woo_add_tab_permissions', 10, 2 ); function yikes_woo_add_tab_permissions( $tabs, $product ) { if ( isset( $tabs['additional-tab-1'] ) && ! current_user_can( 'administrator' ) && ! current_user_can( 'editor' ) ) { unset( $tabs['additional-tab-1'] ); } if ( isset( $tabs['additional-tab-2'] ) && ! current_user_can( 'administrator' ) && ! current_user_can( 'editor' ) ) { unset( $tabs['additional-tab-2'] ); } return $tabs; }That worked, thank you so much, for your support and for creating such an awesome plugin 🙂
-
This reply was modified 8 years, 8 months ago by
The topic ‘Hide/Show MULTIPLE Custom tab only to specific role?’ is closed to new replies.