Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ipokkel/fdfeb10a8120777661fbef4e8ed80fa0 to your computer and use it in GitHub Desktop.

Select an option

Save ipokkel/fdfeb10a8120777661fbef4e8ed80fa0 to your computer and use it in GitHub Desktop.
<?php
/**
*
* ===== WARNING =====
* This code recipe is intended for developers and requires extensive knowledge of websites, and WordPress.
* Copying and pasting this recipe into your site as-is won't work - you need to follow the steps outlined in this code.
* If you are unsure, please hire a local WordPress developer or freelance for assistance.
*
* This code recipe is experimental and was built for private use, outside of Paid Memberships Pro and it's support scope.
*
* USE AT OWN RISK
* ===================
*
* Display the last visited non-PMPro page in the confirmation message, OR
* redirect the member back to the last visited page.
*
* Set your preference to either display the link or redirect back to
* the referrer page by setting the return value of the function
* my_pmpro_redirect_to_referring_page_after_checkout_type().
*
*/
// Ensure session starts safely
function pmpro_custom_start_session() {
if ( ! session_id() ) {
session_start();
}
}
add_action( 'init', 'pmpro_custom_start_session', 1 );
// Set redirect type: 'redirect' or 'message'
function my_pmpro_redirect_to_referring_page_after_checkout_type() {
return 'redirect'; // Or 'message'
}
// Customize the confirmation message
function my_pmpro_redirect_to_referring_page_after_checkout_last_visited_message() {
// translators: %s = link
return esc_html__( 'View your last visited page here: %s.', 'pmpro-customizations' );
}
// Add last page link to confirmation page text
function my_pmpro_redirect_to_referring_page_after_checkout_message( $message ) {
$last_url = my_pmpro_redirect_to_referring_page_after_checkout_get_safe_url();
if ( null === $last_url ) {
return $message;
}
$last_visited_message = my_pmpro_redirect_to_referring_page_after_checkout_last_visited_message();
$last_visited_link = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $last_url ), esc_html( $last_url ) );
$message .= "\n<p>" . sprintf( $last_visited_message, $last_visited_link ) . '</p>';
unset( $_SESSION['my_pmpro_last_url_before_checkout'] );
return $message;
}
// Redirect to last page
function my_pmpro_redirect_to_referring_page_after_checkout_redirect( $url ) {
$last_url = my_pmpro_redirect_to_referring_page_after_checkout_get_safe_url();
unset( $_SESSION['my_pmpro_last_url_before_checkout'] );
return $last_url ? $last_url : $url;
}
// Store the last URL visited on each page load
function my_pmpro_redirect_to_referring_page_after_checkout_track() {
if ( empty( $_REQUEST['redirect_to'] ) ) {
return;
}
global $post, $pmpro_pages;
$last_url_viewed = sanitize_text_field( $_REQUEST['redirect_to'] );
if ( $post && $pmpro_pages && in_array( strval( url_to_postid( $last_url_viewed ) ), $pmpro_pages, true ) ) {
return;
}
$_SESSION['my_pmpro_last_url_before_checkout'] = $last_url_viewed;
}
add_action( 'wp', 'my_pmpro_redirect_to_referring_page_after_checkout_track' );
// Add the filter to the confirmation message or confirmation URL
function my_pmpro_redirect_to_referring_page_after_checkout_filter() {
$type = my_pmpro_redirect_to_referring_page_after_checkout_type();
if ( 'message' === $type ) {
add_filter( 'pmpro_confirmation_message', 'my_pmpro_redirect_to_referring_page_after_checkout_message', 20 );
} elseif ( 'redirect' === $type ) {
add_filter( 'pmpro_confirmation_url', 'my_pmpro_redirect_to_referring_page_after_checkout_redirect' );
}
}
add_action( 'init', 'my_pmpro_redirect_to_referring_page_after_checkout_filter' );
// Retrieve the last URL safely
function my_pmpro_redirect_to_referring_page_after_checkout_get_safe_url() {
if ( empty( $_SESSION['my_pmpro_last_url_before_checkout'] ) ) {
return null;
}
$url = sanitize_text_field( $_SESSION['my_pmpro_last_url_before_checkout'] );
if ( ! wp_http_validate_url( $url ) ) {
return null;
}
return $url;
}
function my_pmpro_custom_no_access_message_body( $body, $level_ids ) {
$body = str_replace( '!!levels_page_url!!', '!!levels_page_url!!?redirect_to=!!referrer!!', $body );
return $body;
}
add_filter( 'pmpro_no_access_message_body', 'my_pmpro_custom_no_access_message_body', 10, 2 );
@ipokkel
Copy link
Author

ipokkel commented Jul 16, 2025

===== WARNING =====

This code recipe is intended for developers and requires extensive knowledge of websites, and WordPress, please hire a local WordPress developer or freelancer for assistance.

This code recipe is experimental and was built for private use, outside of Paid Memberships Pro and it's support scope.

USE AT OWN RISK !!!

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