Bypassing Groups Restrictions

Introduction

This tutorial is a follow-up to “Create a Premium Social Network with WooCommerce“.

The Groups plugin allows you to restrict content to a specific set of your members. But there might be times you want to display the content’s excerpt or featured image to entice new users to sign up for your site. A possible solution is to use Groups’ shortcodes; alternatively, you might find that handling this programmatically is a better solution. This tutorial will guide you through removing restrictions for specific pages or posts by way of your functions.php file so that excerpts can be displayed for non-members.

If you followed the Social Network tutorial (or not), you probably have some content that is protected by Groups Access Restrictions (see screenshot). By default, that content isn’t showing up in your recent posts widget (or anywhere) when the user isn’t logged in. We’re going to make a few changes so that we can tease the premium content on our site’s home page (see screenshot).

Groups Access Restrictions

Latest Posts


The Main Code

Open up your theme’s functions.php file. If you’re using a child theme, open that one. Add the following code:

// Add an action to pre_get_posts
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts() {

	// If the user is looking at the front page
	if ( is_front_page() ) {

		// Remove the Groups_Post_Access filters
		remove_filter( 'posts_where', array( 'Groups_Post_Access', 'posts_where' ), 10, 2 );
		remove_filter( 'the_posts', array( 'Groups_Post_Access', 'the_posts' ), 1, 2 );
		remove_filter( 'get_the_excerpt', array( 'Groups_Post_Access', 'get_the_excerpt' ), 1 );
		remove_filter( 'the_content', array( 'Groups_Post_Access', 'the_content' ), 1 );
	}
}

The first thing we’re doing in the code is hooking into the pre_get_posts action and running a function. So just before our site goes off to the database to get the posts, the my_pre_get_posts function will execute.

Inside my_pre_get_posts, we’re adding a conditional statement to see if the current page is the site’s front page.

Next, we’re simply removing four filters that are set by the Groups plugin. It’s these filters that enforce the content restrictions. So in our code, we’re removing those restrictions for the home page only.

!Important It’s important to understand that all restrictions are being removed from the home page, including widgets.

That’s it! If you’re only wanting to show protected excerpts on the home page, you’re finished. Widgets such as Latest Post and Recent Posts will now display excerpts from your protected content.

 


Code Options

Maybe you’ve decided you want to show the post excerpts for the home page, archive pages, and category pages. If so, change this

if ( is_front_page() ) {

to this

if ( is_archive() || is_category() || is_front_page() ) {

The code change simply checks if an archive page, category or the front page is being viewed. This means that your new user will be able to see the protected excerpts on nearly everything except a single post.

Another option might be to remove the restrictions on a certain page, such as your blog page, so that you can show excerpts in the list of blog posts. This is helpful when your settings in WordPress -> Settings -> Reading -> Front page displays is set to “A static page” (such as in our Social Network tutorial). In this case, you can change this line

if ( is_front_page() ) {

to this

if ( is_page('blog-page') ) {

where “blog-page” is the slug of your blog page.

This code change simply says “If the user is looking at a page who’s slug is ‘blog-page’, remove the restrictions.

 


Next Step – The Links

You might noticed that the links (post title, read more, etc.) leading to your premium content are resulting in a 404 – page not found – for non logged-in users. You see, the Groups plugin prevents the protected post from even being retrieved from the database. Thus, you end up with a 404. There are several ways to make this more user friendly, one of which is by using the Groups 404 plugin to redirect the user to a login page. Setting up this plugin will be the topic of our next tutorial.

 

Leave a Reply

3 Comments on "Bypassing Groups Restrictions"

Notify of
avatar

Sort by:   newest | oldest
Asllan Maciel
Guest
Asllan Maciel
10 years 3 months ago

No way to change the css style or class, only restricted by the plugin groups posts?

rob
Guest
9 years 10 months ago

Legend, really useful for my site and just what I needed!

andrew
Guest
andrew
9 years 4 months ago

This is a brilliant tutorial… worked great.

Thanks!

wpDiscuz