Skip to content

Instantly share code, notes, and snippets.

@cklosowski
Created May 31, 2019 23:58
Show Gist options
  • Save cklosowski/0a4cc56d0a54f9d84d82bdd5319e3c6f to your computer and use it in GitHub Desktop.
Save cklosowski/0a4cc56d0a54f9d84d82bdd5319e3c6f to your computer and use it in GitHub Desktop.
Selectivly start Sessions with Easy Digital Downloads
<?php
/**
* If the page loaded is the homepage, we don't need to start a session if one doesn't exist
*/
function eddwp_ck_maybe_start_session( $start_session ) {
// Doesn't start sessions on the homepage.
if ( '/' == $_SERVER['REQUEST_URI'] ) {
$start_session = false;
}
// Don't start sessions on EDD Software Licensing checks.
$to_skip = array(
'activate_license',
'deactivate_license',
'check_license',
'checkin',
'get_version'
);
if( ! empty( $_REQUEST['edd_action'] ) && in_array( $_REQUEST['edd_action'], $to_skip ) ) {
$start_session = false;
}
// Don't start sessions if we're on a blog post or an archive (needs to be customized to whatever your 'blog' URL format is.
if ( strpos( $_SERVER['REQUEST_URI'], '/blog' ) !== false ) {
$start_session = false;
}
// Finally, if there is a discount in the GET parameters, we should always start a session, so it applies correctly.
if ( ! empty( $_GET['discount'] ) ) {
$start_session = true;
}
return $start_session;
}
add_filter( 'edd_start_session', 'eddwp_ck_maybe_start_session', 10, 1 );
@cklosowski
Copy link
Author

@crobertwatson Since the pages for EDD can be named whatever a store owner wants, predicting them is a bit difficult, however if you make some checks right before the end of the function to do things like:
if ( edd_is_checkout() ) {

if ( edd_is_purchase_history_page() ) {

You could then just add a $start_session = true; in those cases. Then just replace that /blog check entirely with $start_session = false; so we can turn it into true only if the last few conditions are met.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment