epl_lazy_session: start the visitor session only when it's needed
Description #
From 3.6, EPL starts a visitor session, and sends its session cookie, only when it stores something in it (such as a form error message) or when the visitor already has a session. Pages for everyone else send no session cookie, so page caches (WP Rocket, host and CDN caches) can cache them. Return false to start a session on every request, as before 3.6.
Usage #
add_filter( 'epl_lazy_session', '__return_false' );
Parameters #
$lazy (bool)
True to start the session only when something is stored in it. Default true.
Examples #
EPL reads this filter early, on plugins_loaded when it uses its own WP_Session storage, or on init when it uses PHP sessions (the default when the server supports them). Add it in a plugin or a must-use plugin. A theme's functions.php loads after plugins_loaded, which is too late for the WP_Session storage.
<?php // wp-content/mu-plugins/epl-session.php // Start the EPL session on every request, as before 3.6. add_filter( 'epl_lazy_session', '__return_false' );
You only need this when custom code expects a session on every request, for example code that reads EPL()->session->get_id() to recognise a visitor before anything is stored (it returns an empty string until a session starts), or code that writes to $_SESSION directly instead of using EPL()->session->set(). Code that stores values with EPL()->session->set() works either way: the first value stored starts the session.
Related: the epl_use_php_sessions filter and EPL_USE_PHP_SESSIONS constant choose PHP sessions or the WP_Session storage, and epl_start_session stops sessions on some addresses (feeds by default).
Source #
lib/includes/class-epl-session.php, in EPL_Session::lazy_session(), checked in EPL_Session::init() and EPL_Session::maybe_start_session(). Since 3.6.
Methods (3.6) #
On EPL()->session (EPL_Session): lazy_session() (this filter), has_session_cookie() (the visitor already has a session), should_start_session(), maybe_start_session() and start_session() (starts one when something is stored with set()), get() / set(). get() without a session returns nothing and sends no cookie.