Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Created March 20, 2023 14:42
Show Gist options
  • Save JarrydLong/33278f8274ff773e04fc6e30ce0319a3 to your computer and use it in GitHub Desktop.
Save JarrydLong/33278f8274ff773e04fc6e30ce0319a3 to your computer and use it in GitHub Desktop.
<?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