• 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 » Filters » Dynamic redirect to referrer in login

Dynamic redirect to referrer in login

Chad Butler · Aug 2, 2013 ·

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..

 

I have a challenge creating a title for this tutorial, so to describe what it covers, I recently had a question regarding how to dynamically put a redirect back to the original content into the link to the login page.  In this instance, the user had customized how they were using the plugin and rather than having the login in place of blocked content as is the default, they had a link to the login page.  

The WP-Member login page (via the shortcode) does accept the WP redirect_to parameter, so that if this parameter is given, it will redirect back to that link upon successful login.  But how to make that work dynamically?  Here is a quick example of how you can filter the content of a post, look for a link to the login page, and add the redirect_to parameter to the end.

This example will use the_content to call the filter function, get_permalink to get the link we want to come back to, and str_replace to do the search and replace.

The login page is now a setting in the plugin’s main options, so you need to make sure that is set.

add_filter( 'the_content', 'login_link_back' );
function login_link_back( $content ) {
	global $wpmem;
	$old = $wpmem->user_pages['login'];
	$new = $wpmem->user_pages['login'] . '?redirect_to=' . get_permalink();
	
	return str_replace( $old, $new, $content );
}

Filters, Tips and Tricks filters, free, get_permalink, str_replace, the_content, tips

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