Created
March 20, 2023 14:42
-
-
Save JarrydLong/33278f8274ff773e04fc6e30ce0319a3 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 //do not copy | |
/** | |
* This recipe will save a utm_source value when a visitor lands on the checkout | |
* page. This then gets stored in order meta upon successful checkout. | |
* | |
* 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/ | |
*/ | |
function mypmpro_store_utm_checkout() { | |
if ( ! empty( $_REQUEST['utm_source'] ) ) { | |
//If utm_source is present in the URL, lets save it in session | |
$_SESSION['pmpro_utm_source'] = sanitize_text_field( $_REQUEST['utm_source'] ); | |
} | |
} | |
add_action( 'pmpro_checkout_preheader', 'mypmpro_store_utm_checkout' ); | |
function mypmpro_save_utm_to_order( $user_id, $morder ) { | |
if( ! empty( $_SESSION['pmpro_utm_source'] ) ) { | |
//If the session is available, lets save to order meta | |
update_pmpro_membership_order_meta( $morder->id, 'utm_source', sanitize_text_field( $_SESSION['pmpro_utm_source'] ) ); | |
//Order is a success, remove the session | |
if( $morder->status == 'success' ) { | |
unset( $_SESSION['pmpro_utm_source'] ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment