• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Blog
    • Basics
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Store
    • Cart
    • Checkout
  • Contact
  • Log In
  • Show Search
Hide Search
Home » Tips and Tricks » Redirect blocked content to a login page

Redirect blocked content to a login page

Chad Butler · Oct 7, 2020 ·

This article is provided free. Find out how you can get full access to premium content, including how-to articles and support forums, as well as priority email support and member exclusive plugin extensions..

 

While the WP-Members plugin is built to automatically replace blocked content with a login and registration form, sometimes it is necessary to redirect blocked content to a specific login page. This is generally the case with builder plugins (unless you refilter the content), or plugins that use post meta for displaying extra content, such as WooCommerce, Calendar/Event plugins, etc.

This post covers some a basic way to provide the same user experience as the default setup, but by redirecting the blocked content to a login page.

Create a login page

The first part of this process is to create a login page.  You can do that by creating a new page and adding the login form shortcode:

[wpmem_form login]

Once you’ve done that, set the location of this page in the plugin’s main options.

Create a redirect for blocked content

Now that you have a login page, you need to set up your site to redirect any blocked content to that login page. We will do this using the wpmem_redirect_to_login() function in the plugin’s API.  Our function’s logic will use WP’s is_user_logged_in() to see if the user is logged in, and the WP-Members wpmem_is_blocked() function to see if the post is blocked.

Even if the explanation of what we are doing doesn’t make sense to you, that’s OK because the code snippet it put together for you. All you have to do is copy it:

add_filter( 'template_redirect', function() {
    if ( wpmem_is_blocked() && ! is_user_logged_in() ) {
        wpmem_redirect_to_login();
    }
});

If a post or page is marked as “blocked” and a user browses to it, this code snippet will check if they are logged in and if not, it will redirect them to the login page.  The redirection function automatically handles applying a “redirect_to” query string in the URL so that the login will return the user back to the original page.

What do I do with that?

If you’re asking, “That’s great and all, but what do I do with that code?” then this section is for you.  You can either use your theme’s functions.php file (which is the “standard” for custom code snippets) or you can create a custom plugin file for containing your site’s custom code snippets.

Regardless of which direction you choose, there is a tutorial post in the “Basics” category to help you know what to do with code snippets from the site.

That’s it! That’s all you need to do.  Now blocked content will direct to the login, then back to the content.  Below are some additional possible configuration ideas you may also want to explore:

Create a registration page

You might want to have a dedicated registration page to direct users to if you configure the things using the above steps.  Doing that is just like creating the login page.

Create a new page, add the [wpmem_form register] shortcode, then set the page location in the plugin’s main options.

Redirecting the login page

If a user goes to the login page directly (i.e. they do not arrive there by being redirected from blocked content), then logging in will put them onto the login page, but in the “logged in state.”  The default logged in state tells them they are logged in and gives them a log out link.  You may prefer to not use this.

One way I like to approach this is to direct a non-specific login to the user profile page.  If you haven’t already done so, create a user profile page. This is the same as the login and register pages mentioned above, but you’ll use the user profile shortcode [wpmem_profile].

Note the URL of that page and in your login form shortcode, specify the “redirect_to” attribute:

[wpmem_form login redirect_to="https://mysite.com/my-profile-page/"]

If there is no “redirect_to” parameter in the URL when the user goes to the login page, they’ll be redirected to the profile page upon successful login.  (Using the complete instructions outlined on this page, the user WILL have a redirect_to parameter in the URL if they arrive on the login because they went to a blocked page. They will not if they simply browse directly to the login page.)

Tips and Tricks wpmem_is_blocked, wpmem_redirect_to_login

Ready to get started?

Join Today!

© 2025 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Privacy Policy
  • Refund Policy