Membership protected content page dynamic redirect

I have a login form that appears on the protected content page. Is it possible to have the login form redirect to the page that the member was requesting when the protected content page shows up? I don’t see a way to modify the redirect URL dynamically. Please let me know I’m missing something. $_SERVER is blank as is wp_get_referer() for the login form shortcode I’ve created. I’d hate to rewrite the function because I don’t want it overwritten on an update.

  • Ryan Englin
    • Design Lord, Child of Thor

    Real simple login shortcode –

    function pmn_login_form_shortcode( $atts, $content = null ) {

    extract( shortcode_atts( array(
    'redirect' => ''
    ), $atts ) );

    if (!is_user_logged_in()) {
    if($redirect) {
    $redirect_url = $redirect;
    } else {
    $redirect_url = get_permalink();
    $form = wp_login_form(array('echo' => false, 'redirect' => $redirect_url,'label_log_in' => __( '' ) ));
    return '<div id="pmn-login">' . $form . '</div>';
    add_shortcode('loginform', 'pmn_login_form_shortcode');

  • Alexander
    • DEV MAN’s Mascot

    I’ve looked into this, and I don’t think this is possible without modifying the plugin.

    There won’t be a referrer because it’s not the browser transitioning from one link to another. PHP is forcing a redirect with a location header.

    If you’d like to modify the plugin, the function that needs rewriting is called membership_redirect_to_protected It can be found in functions.php around line 1058

    All that to say, I’ll add this as a feature request to Membership. Maybe we could have a login form like this included with the plugin officially, and with support for the referring link. I’ll move this to features and feedback as well as touch base with the developer about it.

    Best regards,

  • Eugene Manuilov
    • Syntax Hero

    Hi Ryan,

    As fast workaround for your issue, try to add this hook to your plugin/theme:

    add_filter( 'wp_redirect', 'membership_back_redirect_from_protected_page' );
    function membership_back_redirect_from_protected_page( $location ) {
    global $M_options;

    $protected_page_url = get_permalink( (int)$M_options['nocontent_page'] );
    if ( $protected_page_url == $location ) {
    // redirecting to protected page, then add backref
    $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $location = add_query_arg( 'redirect', urlencode( $current_url ), $location );

    return $location;

    And then your shortcode should look like this:

    add_shortcode( 'loginform', 'pmn_login_form_shortcode' );
    function pmn_login_form_shortcode( $atts, $content = null ) {
    extract( shortcode_atts( array( 'redirect' => '' ), $atts ) );

    if ( !is_user_logged_in() ) {
    if ( $redirect ) {
    $redirect_url = $redirect;
    } else {
    $redirect_url = isset( $_GET['redirect'] ) ? $_GET['redirect'] : get_permalink();

    $form = wp_login_form( array( 'echo' => false, 'redirect' => $redirect_url ) );

    return '<div id="pmn-login">' . $form . '</div>';

    To be honest I haven’t tested it, but I suppose it should work fine. Let me know if it doesn’t work for you.


  • Ollie
    • The Incredible Code Injector

    @eugene Manuilov : thank you I have given you some points as additional thanks :slight_smile:

    @ryan Englin : Thanks for raising this issue, I have been looking for solutions for about a month now…

    WPMU Staff: Please add this to the core plugin, its so important.. here is the frustration for customers/end users:

    User receives a Q&A or other link via email that’s behind protected content. The user clicks the link from the email and gets redirected to protected content page to login, they login and it does the usual process of redirection after login. The user would need to re find the original link they clicked, It’s messy out of the box…

    This worked nicely, it’s a shame we have to do a custom fix here because this feels like an essential feature of membership that is missing. :slight_smile:



  • Eugene Manuilov
    • Syntax Hero

    Hi @Billzy

    You are most welcome! I have added this fix to the core of the plugin. So it will be included in the next release.

    P.S.: if you want to render login form on the protected content page, then you can just use [membershiplogin] shortcode which is already built in the plugin core.



Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.