Last active
March 21, 2024 10:40
-
-
Save ipokkel/fdfeb10a8120777661fbef4e8ed80fa0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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. | |
* | |
* 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_url!! 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!!?redirect_to=!!referrer!!">Join Now</a> | |
*/ | |
// 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() { | |
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 | |
add_action( 'wp', 'my_pmpro_redirect_to_referring_page_after_checkout_track' ); | |
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 the filter to the confirmation message or confirmation URL | |
add_action( 'init', 'my_pmpro_redirect_to_referring_page_after_checkout_filter' ); | |
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' ); | |
} | |
} | |
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment