How to Show Different Menus to Different User Roles in Woocommerce - wordpress

I want to hide Downloads from Woocommerce menu when user type is Artist.

Add this code to your theme's 'functions.php'.
function custom_my_account_menu_items( $items ) {
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$role = ( array ) $user->roles;
if($role[0]=="artist") // change role name if different
unset($items['downloads']);
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );

Related

Removing price suffix based on user role

Having failed at adding a suffix based on User Role because it wont display the Woo price display shortcode, I'm now approach the problem from the other direction - I have added the suffix to Woos tax tab, and now instead want to remove the suffix (from all user roles except one).
I found this code on github to remove the suffix from products:
add_filter( 'woocommerce_get_price_suffix', 'custom_woocommerce_get_price_suffix', 10, 2 );
function custom_woocommerce_get_price_suffix( $price_display_suffix, $product ) {
if ( ! $product->is_taxable() ) {
return '';
}
return $price_display_suffix;
}
and I have modified it to hide the suffix from certain user types
add_filter( 'woocommerce_get_price_suffix', 'custom_woocommerce_get_price_suffix', 10, 2 );
function custom_woocommerce_get_price_suffix( $price_display_suffix, $product ) {
// check current user role
$user = wp_get_current_user();
$roles = ( array ) $user->roles;
if ( in_array( 'administrator', $roles ) ) {
$price = $your_suffix;
} elseif ( in_array( 'default_wholesaler', $roles ) ) {
$price = '$your_suffix';
return '';
}
return $price_display_suffix;
}
This worked, however I had to switch the users (I want Admin and Wholesalers to see the suffix) and put in Customers, etc.
The problem is that guest customers still see the suffix.
Could someone suggest a way of hiding the suffix from anyone except the logged in 'default-wholesaler' user and 'Administrator'?
Thanks!
You could use the following for this.
This works for the administrator role! you can add the other role yourself as an 'exercise'
function custom_woocommerce_get_price_suffix( $html, $product, $price, $qty ) {
// check current user role
$user = wp_get_current_user();
$roles = ( array ) $user->roles;
// if NOT in array user roles
if ( !in_array( 'administrator', $roles ) ) {
$html = '';
}
return $html;
}
add_filter( 'woocommerce_get_price_suffix', 'custom_woocommerce_get_price_suffix', 10, 4 );

Pop up Message when clicking Woocommerce Categories

I want a pop up message to appear when a user without a certain postal code tries to look at a certain category.
I have gotten as far as to redirect the user if there postal code is not in the allowed array.
I would just like it to state "You cannot access this category" and without any redirect.
function my_restrict_access() {
if ( is_product_category() ) {
$category = get_queried_object();
if ( $category ) {
$category_id = $category->term_id;
$allowed_category_id = array(3078, 3385);
if ( in_array($category_id, $allowed_category_id) ) {
if ( !is_user_logged_in() ) {
wp_redirect( '/' );
exit;
} else {
global $woocommerce;
$customer = new WC_Customer();
$customer_postcode = $woocommerce->customer->get_billing_postcode();
// uncomment line below for debug purposes, this will show you the postcode on the current page
//echo $customer_postcode;
$allowed_post_code = array(3610, 3626, 3650);
if ( !in_array($customer_postcode, $allowed_post_code) ) {
wp_redirect( '/' );
exit;
}
}
}
}
}
}
add_action( 'template_redirect', 'my_restrict_access');
You better need to use wp filter the_content to change front content...
add_action( 'the_content', 'restrict_user_to_certain_shop_category' );
function restrict_user_to_certain_shop_category( $content ){
global $woocommerce;
// list of all allowed woo product categories
$allowed_categories = array( 3078, 3385 );
// allowed user whose postcodes are following
$allowed_post_codes = array( 3610, 3626, 3650 );
// check if current viweing page is product archive page
if ( is_product_category() ) {
$current_category = get_queried_object();
// extract archive category id
$current_category_id = $current_category->term_id;
// check if current category is in allowed array list
if ( in_array( $current_category_id, $allowed_categories ) ) {
// check if user is not logged in
if ( ! is_user_logged_in() ) {
// message to show when user is not logged in and viewing allowed product category
$content = 'You cannot access this category';
}
// user is logged in and viewing allowed product category
else {
$customer = new WC_Customer();
$customer_postcode = $woocommerce->customer->get_billing_postcode();
// check if user is from allowed postarea
if ( !in_array( $customer_postcode, $allowed_post_codes ) ) {
// message to show when user is logged in and viewing allowed product category + from allowed postarea
$content = 'You cannot access this category';
}
}
}
}
return $content;
}

Wordpress - Make it so that non-admin users can't see posts with a specific custom post status

What hooks would I use for the functions file in order to make it so that all non-admin users can't see all posts with a specific custom post_status in the wp-admin back-end. BUT it is still able to be queried and looped through the WordPress post loop?
With pre_get_posts you should be able to get started (to hide posts from the admin screen). You may also want to check the post type, etc.
function filter_posts( $wp_query ) {
if ( is_admin() ) {
$user = wp_get_current_user();
$post_status = 'draft';
if ( ! in_array( 'administrator', $user->roles ) ) {
$wp_query->set( 'post_status', $post_status );
}
}
}
add_action( 'pre_get_posts', 'filter_posts', 10 );
To disallow users to edit posts with that specific status, you should do:
function restrict_post_editing(){
global $post;
$post_status = 'draft';
if ( get_post_status( $post ) == $post_status ) {
$user = wp_get_current_user();
if ( ! in_array( 'administrator', $user->roles ) ) {
do_action('admin_page_access_denied');
wp_die( __('You cannot modify or delete this entry.') );
exit;
}
}
}
add_action('edit_post', 'restrict_post_editing', 10, 1);
add_action('wp_trash_post', 'restrict_post_editing', 10, 1);
add_action('before_delete_post', 'restrict_post_editing', 10, 1);

WordPress SEO plugin only visible to specific user

I am using the Yoast SEO plugin in WordPress and wanted to know if there was a way to make it only visible to one specific user in the db or in the functions.php file? Not a role, an actual user.
I tried an universal solution to simply add "plugin-name" and disable it, but failed.
But, to show WPSEO only to a specific user (ID equals 2), the following works:
add_action( 'plugins_loaded', 'seo_so_25654837' );
function seo_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
remove_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
}
Don't add the code to functions.php, use it as a normal plugin.
The following is also needed to remove the SEO menu from the admin bar:
add_action( 'wp_before_admin_bar_render', 'bar_so_25654837' );
function bar_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
global $wp_admin_bar;
$nodes = $wp_admin_bar->get_nodes();
foreach( $nodes as $node )
{
if( !$node->parent )
{
if( 'wpseo-menu' === $node->id )
$wp_admin_bar->remove_menu( $node->id );
}
}
}
You can hook to pre_current_active_plugins to remove elements from the table before it is displayed. Using get_current_user_id() within the function will let you selectively hide a plugin.
function hide_plugins_by_user( $all_plugins=false ) {
global $wp_list_table;
// if the current user ID is not 1, hide it.
if ( 1 != get_current_user_id() ){
// the active plugins from the table
$plugins = $wp_list_table->items;
// loop through them
foreach ( $plugins as $key => $val ) {
// use the dir + filename of the plugin to hide
if ( $key == 'plugindir/plugin.php' ) {
unset( $wp_list_table->items[$key] );
}
}
}
}
add_action( 'pre_current_active_plugins', 'hide_plugins_by_user' );
This code is working fine (Credits goes to Hislop):
// Returns true if user has specific role
function check_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) )
$user = get_userdata( $user_id );
else
$user = wp_get_current_user();
if ( empty( $user ) )
return false;
return in_array( $role, (array) $user->roles );
}
// Disable WordPress SEO meta box for all roles other than administrator and seo
function wpse_init(){
if( !(check_user_role('seo') || check_user_role('administrator')) ){
// Remove page analysis columns from post lists, also SEO status on post editor
add_filter('wpseo_use_page_analysis', '__return_false');
// Remove Yoast meta boxes
add_action('add_meta_boxes', 'disable_seo_metabox', 100000);
}
}
add_action('init', 'wpse_init');
function disable_seo_metabox(){
remove_meta_box('wpseo_meta', 'post', 'normal');
remove_meta_box('wpseo_meta', 'page', 'normal');
}
Just place it in the functions.php file.
To disable the Yoast for all the users and enable it for just for few or specific, just add the following piece of code to your function.php file.
function remove_wpseo(){
/* if you want to keep it enabled for user with id 2 */
if ( '2' == get_current_user_id() ) {
return;
}
global $wpseo_front;
if(defined($wpseo_front)){
remove_action('wp_head',array($wpseo_front,'head'),1);
}
else {
$wp_thing = WPSEO_Frontend::get_instance();
remove_action('wp_head',array($wp_thing,'head'),1);
}
}
add_action('template_redirect','remove_wpseo');
Reference: https://makersbyte.com/disable-yoast-seo-plugin-specific-page/

Display role of administrator next to the username in WordPress

Is there a way to display an extra text next to the Username if the person has the administrator role?
I just want to display the extra text for this role only, what hook could I use for that?
Yes, you can use the filter the_author, like so:
add_filter( 'the_author', 'author_admin_so_14367149' );
function author_admin_so_14367149( $display_name )
{
// Block the filter in the dashboard (admin area)
if( is_admin() )
return $display_name;
global $authordata;
// User has administrator role, add string to display name
if( in_array( 'administrator', $authordata->roles ) )
$display_name = $display_name . ' (admin)';
return $display_name;
}
For the comments section, there's another filter and the checks are different:
add_filter( 'get_comment_author', 'comment_author_admin_so_14367149' );
function comment_author_admin_so_14367149( $author )
{
// Block the filter in the dashboard (admin area)
if( is_admin() )
return $author;
$user = get_user_by( 'login', $author );
// User has administrator role, add string to display name
if( in_array( 'administrator', $user->roles ) )
$author = $author . ' (admin)';
return $author;
}

Resources