WooCommerce Memberships: How to allow viewing all front end restricted content to any user or by a user role.

In this tutorial I will show you how WC Memberships restrictions work. We will create a functions.php filter that allows to view all restricted content to any user.

September 27, 2016 / #functions.php, #Roles and Caps, #WooCommerce, #WooMemberships

Where is the problem with the «Editor» role and WC Memberships?

Let’s look at the «Editor» role on the website with WooCommerce Memberships installed.

Editors can edit any content-based custom post types. Even if the post is restricted by WC Membership.

But when they’re trying to preview changes of their restricted post, they see this error message:

Restricted content WooCommerce Memberships

Quiet a nonsense. User can edit but can not view.

In this totorial I will try to explain you why this happens and how to fix it.

Just interesting – what are you doing in this case?

Have you ever been faced with that problem?

If yes – what did you do to fix it? Did you create a special membership for editors?

How to fix it the easy way?

Intro

I begin with saying that administrator and shop_manager are two roles that have overall website content access without dependency on content restriction.

Let’s begin with that point.

It took mу 3 hours of looking through the WC Memberships code to understand, that the plugin uses user_has_cap filter hook to allow complete access for administrators and shop managers.

How?

Simple — it just checks user manage_woocommerce capability. So, if current_user_can( 'manage_woocommerce' ) this user has the overall website content access in front end.

So, you can just run this code at once:

$editor = get_role( 'editor' );
$editor->add_cap( 'manage_woocommerce');

The effect will be OK, but you will notice that in admin area editor receive a capability of managing WooCommerce settings.

WooCommerce settings access

Well, we do not want that! So, let’s come back and remove manage_woocommerce capability.

$editor->remove_cap( 'manage_woocommerce');

Ready-to-use code for your functions.php

Finally, the solution. Just insert it and enjoy.

/*
 * Allow editor to view all restricted content
 */
add_filter( 'user_has_cap', 'misha_add_editor_cap', 20, 3 );
 
/*
 * @param array $allcaps All user capabilities
 * @param array $caps Current capability we're filtering now - $caps[0]
 * @param array $args Capability parameters, e.g. $args[0] - capability name, $args[1] - user ID, $args[2] - post ID
 */
function misha_add_editor_cap( $allcaps, $caps, $args ) {
	if ( isset( $caps[0] ) )  :
 
		switch ( $caps[0] ) :
			case 'wc_memberships_access_all_restricted_content':
			case 'wc_memberships_view_restricted_post_content' :
			case 'wc_memberships_view_restricted_product' :
			case 'wc_memberships_view_restricted_product_taxonomy_term':
			case 'wc_memberships_view_delayed_product_taxonomy_term':
			case 'wc_memberships_view_restricted_taxonomy_term' :
			case 'wc_memberships_view_restricted_taxonomy' :
			case 'wc_memberships_view_restricted_post_type' :
			case 'wc_memberships_view_delayed_post_type':
			case 'wc_memberships_view_delayed_taxonomy':
			case 'wc_memberships_view_delayed_taxonomy_term':
			case 'wc_memberships_view_delayed_post_content' :
			case 'wc_memberships_view_delayed_product' :
			//case 'wc_memberships_purchase_delayed_product' :
			//case 'wc_memberships_purchase_restricted_product' :
 
				// if you want to apply changes only for specific user, use:
				// if( get_current_user_id() == 2451 ) // 2451 - User ID
				// or
				// if( $args[1] = 2451 )
				// check if Editor
				if ( $allcaps['editor'] === true ) {
					$allcaps[ $caps[0] ] = true;
					break;
				}
 
			break;
		endswitch;
 
	endif;
 
	return $allcaps;
 
}

If you have a question or a suggestion, please scroll down to comments.

Only the best of WordPress

once a week, no spam

Leave your question or feedback

phpjsHTMLCSSSQLCode
Please, enter a comment
Please, enter a name
Incorrect email