Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Created November 25, 2024 11:52
Show Gist options
  • Select an option

  • Save andrewlimaza/eddc8ba56dd89aee6b32dd85391271d8 to your computer and use it in GitHub Desktop.

Select an option

Save andrewlimaza/eddc8ba56dd89aee6b32dd85391271d8 to your computer and use it in GitHub Desktop.
Redirect back to AddOn Packages using Javascript on PMPro Confirmation Page.
<?php
/**
* Redirect members that bought an Addon Package back to this page using JavaScript.
* Add this code to your site by following this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_custom_redirect_confirmation_page() {
global $pmpro_pages;
// Only load this on the confirmation PMPro page.
if ( ! is_page( $pmpro_pages['confirmation'] ) ) {
return;
}
$url = '';
//Let's only do this if Addon Packages is active and a package id is passed.
if ( function_exists( 'pmproap_hasAccess' ) ) {
if ( isset( $_REQUEST['ap'] ) && ! empty( $_REQUEST['ap'] ) ) {
$ap = get_post( $_REQUEST['ap'] );
} else {
// Let's get the $ap post from the order notes.
$order = new MemberOrder();
$order->getLastMemberOrder( $user_id, array( 'success' ) );
preg_match( '/Addon Package:(.*)\(#(\d+)\)/', $order->notes, $matches );
$pmproap_ap_id = $matches[2];
if ( ! empty( $pmproap_ap_id ) ) {
$ap = get_post( $pmproap_ap_id );
}
}
if ( $ap ) {
$url = get_permalink( $ap->ID );
}
}
if ( ! empty( $url ) ) {
?>
<script>
window.location.href = '<?php echo esc_url( $url ); ?>';
</script>
<?php
}
}
add_action( 'wp_head', 'my_pmpro_custom_redirect_confirmation_page' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment