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 );
@crobertwatson
Copy link

Is there a way to reverse the sense of the statement on line 27 to only start a session if it's one of the specific EDD URLs such as /checkout or /purchase-history that needs it? I've tried but can't get it to work without the checkout breaking. I have a bunch of pages and posts that permalink at / and not at something easy to select for like /blog or /pages. I don't want EDD sessions starting on those because the pages load more slowly. I just want the sessions on the EDD-specific URLs that require it.

@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