Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ipokkel/619a73a41f1cdba9fe8285858fe9731b to your computer and use it in GitHub Desktop.
Save ipokkel/619a73a41f1cdba9fe8285858fe9731b to your computer and use it in GitHub Desktop.
Redirect new members and existing members that changes their existing level back to the referring page after checkout.
<?php
/**
* This gist is a placeholder for historic purposes.
* And updated and improved version may be found here:
* @link https://gist.github.com/ipokkel/32d6ba9425d4550a5861709498e00135
*/
// Original recipe with improved logic to avoid redirecting to PMPro pages:
// -------------------------------------------------------------------------
/**
* Display the last visited non-PMPro page in the confirmation message, OR
* redirect the member back to the last visited page.
*
* Usage Guide:
*
* 1. Set your preference to either display the link or redirect back to
* the referrer page by setting the $toggle_display_redirect variable
* to true or false.
* 2. Customize your message for the confirmation page in the $referrer_message
* variable.
*
* This recipe requires that the Message for Logged-out Users and
* Message for Logged-in Non-members setting on the
* Advanced Settings page (Memberships > Settings > Advanced) has
* "?redirect_to=!!referrer!!" after the !!levels_page!! placeholder.
*
* E.g <a href="!!levels_page_url!!?redirect_to=!!referrer!!">Join Now</a>, or
* <a href="!!login_url!!?redirect_to=!!referrer!!">Login</a> <a href="!!levels_page_url!!">Join Now</a>
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_display_link_or_redirect_to_last_page_after_checkout() {
// set to true to display the link in the confirmation message OR set to false to redirect.
$toggle_display_redirect = true;
// Bail if PMPro or required custom functions not available.
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) || ! function_exists( 'my_pmpro_add_previous_page_link_to_confirmation_message' ) || ! function_exists( 'my_pmpro_confirmation_redirect_to_last_post_viewed' ) ) {
return;
}
if ( $toggle_display_redirect ) {
add_filter( 'pmpro_confirmation_message', 'my_pmpro_add_previous_page_link_to_confirmation_message', 20, 1 );
} else {
add_filter( 'pmpro_confirmation_url', 'my_pmpro_confirmation_redirect_to_last_post_viewed', 10, 3 );
}
}
add_action( 'init', 'my_pmpro_display_link_or_redirect_to_last_page_after_checkout' );
/*
* Remember referring page
*/
function last_post_viewed_cookie() {
global $wp;
$rurl = '';
if ( isset( $_REQUEST['redirect_to'] ) && ! empty( $_REQUEST['redirect_to'] ) ) {
$rurl = $_REQUEST['redirect_to'];
} else {
$rurl = home_url( add_query_arg( array(), $wp->request ) );
}
if ( empty( $rurl ) ) {
return;
}
global $post, $pmpro_pages;
//don't track pmpro pages
if ( in_array( strval( url_to_postid( $rurl ) ), $pmpro_pages, true ) ) {
return;
}
setcookie( 'last_post_viewed', $rurl, null, '/' );
}
add_action( 'wp', 'last_post_viewed_cookie' );
/*
* Add last page link to confirmation page text.
*/
function my_pmpro_add_previous_page_link_to_confirmation_message( $message ) {
$url = false;
if ( isset( $_COOKIE['last_post_viewed'] ) && ! empty( $_COOKIE['last_post_viewed'] ) ) {
$url = $_COOKIE['last_post_viewed'];
}
// Add URL to message if it's considered safe.
if ( wp_http_validate_url( $url ) ) {
// Customize your message here
/* translators: %s: URL of last visited page */
$referrer_message = '<p>' . sprintf( __( 'View your last visited page here: %s.', 'pmpro-customizations' ), '<a href="' . $url . '">' . $url . '</a>' ) . '</p>';
// Add to the message
$message .= $referrer_message;
}
return $message;
}
/*
* Redirect to last page instead of confirmation page after checkout.
*/
function my_pmpro_confirmation_redirect_to_last_post_viewed( $url, $user_id, $level ) {
if ( isset( $_COOKIE['last_post_viewed'] ) && ! empty( $_COOKIE['last_post_viewed'] ) ) {
$new_url = $_COOKIE['last_post_viewed'];
}
// Change confirmation URL if it's considered safe.
if ( wp_http_validate_url( $new_url ) ) {
$url = $new_url;
}
return $url;
}
@ipokkel
Copy link
Author

ipokkel commented Jun 6, 2022

Reviewed/updated gist here: sc0ttkclark/c46cc0f1abdafd07a853787c858badfe

Some users reported the recipe not redirecting after checkout.
Updated working gist here: https://gist.github.com/ipokkel/32d6ba9425d4550a5861709498e00135

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment