• 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 » Code Snippets » Redirect all pages to login

Redirect all pages to login

Chad Butler · Apr 25, 2019 ·

// Redirects all pages except login, register, and profile url to the login page.
// (note: the profile url is needed for password reset which is why it is excluded).

/** 
 * Use 'template_redirect' if you might need to use additional functions for
 * logical tests that need $post data, like is_page()
 * @see: https://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect
 */
add_action( 'template_redirect', 'my_redirect_to_login' );
function my_redirect_to_login() {
  // Get an array of user pages with wpmem_user_pages()
  // @see: https://rocketgeek.com/plugins/wp-members/docs/api-functions/wpmem_user_pages/
  $pages = wpmem_user_pages();

  // If the user is not logged in, and the current page is not in the user pages array.
  // @see: https://rocketgeek.com/plugins/wp-members/docs/api-functions/wpmem_current_url/
  if ( ! is_user_logged_in() && ! in_array( wpmem_current_url( true, false ), $pages ) ) {
    // Redirect the user to the login page.
      wpmem_redirect_to_login();
  }
  return;
}

Not sure what to do with this code?

You're not a "coder" and don't know what to do? Don't worry! Code Snippets are the basic building blocks of WordPress customization, and once you know the basics, they are simple to use.

Here are some free articles to get you started:

  • Using Code Snippets from the Site
  • Using a code snippets plugin
  • The functions.php File
  • Create a plugin file for custom functions
  • Create a child theme
  • Do not modify plugin files!

For "hands on" help, consider a plugin support subscription or the Pro Bundle.

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