Created
November 25, 2024 11:52
-
-
Save andrewlimaza/eddc8ba56dd89aee6b32dd85391271d8 to your computer and use it in GitHub Desktop.
Redirect back to AddOn Packages using Javascript on PMPro Confirmation Page.
This file contains hidden or 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 | |
| /** | |
| * 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