Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • 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
/**
* Updated with https://gist.github.com/ipokkel/fdfeb10a8120777661fbef4e8ed80fa0
* This gist is kept for archival purposes, use the updated one above please.
*/
@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