wpf_user_can_access

Overview

This filter is run when determining whether a user can access any piece of content on your site that can be protected by WP Fusion, including:

You can use this filter to create your own dynamic access rules based on criteria that aren’t available through WP Fusion’s meta boxes.

To use the code examples below, add them to your active theme’s functions.php file.

Parameters

  • $can_access: This variable represents whether or not the user can access the content, as determined by your existing access rules. It will either be true or false.
  • $user_id: ID of the current logged in user. Will be false if the user isn’t logged in.
  • $post_id: Post ID for the post being requested. Will be false if the item isn’t a post (for example a Gutenberg block)

See also

  • wpf_user_can_access_post_type: Runs on a post type, in conjunction with wpf_post_type_rules
  • wpf_user_can_access_archive: Runs on a taxonomy term ID instead of a post ID, determines if a user has access to a term archive
  • wpf_user_can_access_widget: Runs on widgets
  • wpf_user_can_access_block: Runs on Gutenberg blocks

Examples

Deny access to posts within a certain category

The example below will allow access to posts in the category “My Category” only to users who have the tag “Special Tag”.

function restrict_post_categories( $can_access, $user_id, $post_id ) {
	
	if ( in_category( 'My Category', $post_id ) && ! wp_fusion()->user->has_tag( 'Special Tag', $user_id ) ) {
		return false;
	} else {
		return true;
	}

}

add_filter( 'wpf_user_can_access', 'restrict_post_categories', 10, 3 );

Restrict past content by registration date

This example denies access to any content that was published before the user’s registration date:

function disallow_before_date_published( $can_access, $user_id, $post_id ) {

	$published = get_the_date( 'U', $post_id );
	$userdata  = get_userdata( $user_id );

	if ( strtotime( $userdata->user_registered ) < $published ) {
		$can_access = false;
	}

	return $can_access;

}

add_filter( 'wpf_user_can_access', 'disallow_before_date_published', 10, 3 );

Restrict past content by date tag applied

As an alternative, you can track the date that a specific tag was applied (for example Paying Member) using the wpf_tags_applied action, and then deny access to any content published before that tag was applied.

This is similar to the functionality with the Restrict Past Content addon by Restrict Content Pro.

function disallow_before_date_published_alt( $can_access, $user_id, $post_id ) {

	$published = get_the_date( 'U', $post_id );
	$startdate = get_user_meta( $user_id, 'startdate', true );

	// If the content was published before the user's startdate field, deny access

	if ( empty( $startdate ) || $startdate < $published ) {
		$can_access = false;
	}

	return $can_access;

}

add_filter( 'wpf_user_can_access', 'disallow_before_date_published_alt', 10, 3 );

function update_tag_applied_timestamp( $user_id, $tags ) {

	// When TAGNAME is applied, save the current time to the startdate field

	if ( in_array( 'TAGNAME', $tags ) ) {
		update_user_meta( $user_id, 'startdate', time() );
	}

}

add_action( 'wpf_tags_applied', 'update_tag_applied_timestamp', 10, 2 );

Restrict content by a user’s age

In this example, any post protected by the tag Adult will require the user to be 18 years old or older to view the post.

For this to work you need to collect the user’s birthday in the date_of_birth usermeta field.

function restrict_content_by_age( $can_access, $user_id, $post_id ) {

	$settings = get_post_meta( $post_id, 'wpf-settings', true ); // Get the settings

	if ( false == $settings || empty( $settings['allow_tags'] ) ) {
		return $can_access; // Not protected by WP Fusion
	}

	$tag_to_check = wpf_get_tag_id( 'Adult' );

	if ( in_array( $tag_to_check, $settings['allow_tags'] ) ) {

		// The post is protected by the "Adult" tag, check the user's age

		$age = strtotime( get_user_meta( $user_id, 'date_of_birth', true ) );

		if ( false == $age ) {

			// The user's age is unknown, access is denied
			return false;

		} else {

			$minimum_age = strtotime( '+18 years', $age );

			if ( time() < $minimum_age ) {

				// The user is not 18 or older, access is denied
				return false;

			} else {

				// The user is old enough, access is granted
				return true;

			}
		}
	}

	return $can_access;

}

add_filter( 'wpf_user_can_access', 'restrict_content_by_age', 10, 3 );

Requiring an onboarding course completion before accessing the rest of the site

This example uses the presence of an Onboarded tag to lock all content on the site until the user has completed a LearnDash course with ID 123.

Configure the LearnDash course to apply the Onboarded tag when it’s marked complete, and the rest of the site content will then be unlocked.

function require_onboarding( $can_access, $user_id, $post_id ) {

	if ( false !== $post_id // If the content being accessed is a post...
		&& ! wpf_has_tag( 'Onboarded' ) // and the user doesn't have the tag Onboarded...
		&& 123 != learndash_get_course_id( $post_id ) // and the content isn't part of the course ID 123...
	) {
		return false; //... then access is denied.
	}

	return $can_access; // Otherwise use the default access rules

}

add_filter( 'wpf_user_can_access', 'require_onboarding', 10, 3 );

Unlock any locked content when visited from a Facebook link

This example bypasses WP Fusion’s access rules when the visitor is coming to the site from a Facebook link.

function unlock_for_fb_referral( $can_access, $user_id, $post_id ) {

	if( isset( $_SERVER['HTTP_REFERER'] ) && strpos( $_SERVER['HTTP_REFERER'], 'facebook') !== false ) {
		return true; // http referrer
	} elseif ( isset( $_GET['fbclid'] ) ) {
		return true; // the ?fbclid= URL parameter
	}

	return $can_access;

}

add_filter( 'wpf_user_can_access', 'unlock_for_fb_referral', 10, 3 );

Bypass query filtering on a specific post

When WP Fusion’s query filtering is enabled, any posts that a user doesn’t have access to will be completely hidden from your site, including all listings, course grids, navigation, archives, etc.

There may be cases when you want to exclude a single post from being hidden. For example you might want to show a restricted course in your course catalog, so that when the user clicks on it they’re redirected to a sales page.

By checking if the pre_get_posts action is currently running, we can tell if WP Fusion is filtering the query results, and exclude specific items from being hidden. In this example the post with ID 123 is excluded if the current user has the tag “Group A”.

function bypass_query_filtering( $can_access, $user_id, $post_id ) {

	if ( doing_action( 'pre_get_posts' ) ) {

		if ( wpf_has_tag( 'Group A', $user_id ) && 123 == $post_id ) {
			$can_access = true;
		}
	}

	return $can_access;

}

add_filter( 'wpf_user_can_access', 'bypass_query_filtering', 10, 3 );

Unlock everything

Sometimes you might want to temporarily deactivate WP Fusion’s access controls, either for troubleshooting, or a special “open day” promotion. This code disables all WP Fusion access rules and grants access to everything.

add_filter( 'wpf_user_can_access', '__return_true' );

Was this helpful?