Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ipokkel/32d6ba9425d4550a5861709498e00135 to your computer and use it in GitHub Desktop.
Save ipokkel/32d6ba9425d4550a5861709498e00135 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
/**
* 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>
*
* 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/
*/
/**
* This is the place you will set what kind of redirect to use.
*
* @return string The kind of redirect to use.
*/
function my_pmpro_redirect_to_referring_page_after_checkout_type() {
/*
* Set this to 'redirect' if you would like redirect on order confirmation,
* or set this to 'message' if you want to show the last URL in the confirmation message.
*/
return 'redirect';
}
/**
* This is the place you will set confirmation message you want to show (if type is 'message').
*
* @return string The last visited message to show.
*/
function my_pmpro_redirect_to_referring_page_after_checkout_last_visited_message() {
// translators: %s: The linked URL of the last visited page.
return esc_html__( 'View your last visited page here: %s.', 'pmpro-customizations' );
}
/**
* Add last page link to confirmation page text.
*
* @param string $message The confirmation message.
*
* @return string The updated confirmation message.
*/
function my_pmpro_redirect_to_referring_page_after_checkout_message( $message ) {
$last_url = my_pmpro_redirect_to_referring_page_after_checkout_get_safe_url();
// Check if we have a safe last 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 ) );
// Append the customized last visited message to the confirmation message.
$message .= "\n" . '<p> ' . sprintf( $last_visited_message, $last_visited_link ) . '</p>';
return $message;
}
/**
* Redirect to last page instead of confirmation page after checkout.
*
* @param string $url The confirmation page URL to redirect to.
*
* @return string The updated confirmation page URL to redirect to.
*/
function my_pmpro_redirect_to_referring_page_after_checkout_redirect( $url ) {
$last_url = my_pmpro_redirect_to_referring_page_after_checkout_get_safe_url();
// Check if we have a safe last URL.
if ( null === $last_url ) {
return $url;
}
// Send the customer to the last known URL on confirmation.
return $last_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;
}
$last_url_viewed = $_REQUEST['redirect_to'];
global $post, $pmpro_pages;
// Do not track the URL if we are on one of the PMPro pages.
if ( $post && $pmpro_pages && in_array( strval( url_to_postid( $last_url_viewed ) ), $pmpro_pages, true ) ) {
return;
}
setcookie( 'my_pmpro_last_url_before_checkout', $last_url_viewed, null, '/' );
}
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' );
/**
* Get the last URL before checkout stored in the cookie.
*
* @return null|string The last URL if safe, otherwise null.
*/
function my_pmpro_redirect_to_referring_page_after_checkout_get_safe_url() {
// Check if there is a last URL stored.
if ( empty( $_COOKIE['my_pmpro_last_url_before_checkout'] ) ) {
return null;
}
$url = $_COOKIE['my_pmpro_last_url_before_checkout'];
// Check if the last URL is safe.
if ( ! wp_http_validate_url( $url ) ) {
return null;
}
return $url;
}
@GiannisRallis
Copy link

GiannisRallis commented Aug 29, 2023

i tried to fix something like this one but not working


// Hook into the checkout completion event
function custom_pmpro_after_checkout( $user_id ) {
	// Retrieve membership levels for the user
    $membership_levels = apply_filters( 'pmpro_get_membership_levels_for_user', $levels, $user_id );

    // Assuming a mapping of membership level IDs to page IDs
    $membership_page_mapping = array(
        1 => 'https://url.gr/courses/nomiki-athinon-fast/',
        2 => 'https://url.gr/courses/nomiki-athinon-silver/',
		3 => 'page-url-for-level-2',
		4 => 'https://url.gr/courses/nomiki-athinon-gold/',
		5 => 'page-url-for-level-2',
		6 => 'page-url-for-level-2',
		7 => 'page-url-for-level-2',
        // ... Add more mappings as needed
    );

    // Find the purchased membership level and redirect
    foreach ( $membership_levels as $level ) {
        if ( array_key_exists( $level->id, $membership_page_mapping ) ) {
            $redirect_url = $membership_page_mapping[ $level->id ];
            wp_redirect( $redirect_url );
            exit;
        }
    }
}
add_action( 'pmpro_after_checkout', 'custom_pmpro_after_checkout' );

@GiannisRallis
Copy link

Your code is not work on my website :(

@ipokkel
Copy link
Author

ipokkel commented Mar 21, 2024

@GiannisRallis This is experimental code that is intended to redirect users back to the last page/post they visited before checking out for membership and is not designed to redirect users to a custom URL per level after checkout.

You should be able to achieve the latter by hooking into the pmpro_confirmation_url filter. Here is a gist that you or your developer may use as an example to tweak to suit your site-specific requirements - https://gist.github.com/ipokkel/c9fb4f9bece3814ffa5120c23a61b811

I hope this helps get you started. :)

@graicifyd
Copy link

Thank you This works for posts but not for custom post types. I am using WP Job Manager and the listings redirect to a subscription page when visited by a non-member. How can I modify these codes to append the referrer to the job listings so that after checkout the user is redirected back to the previous page? Thank you for your help.

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