• This is a theme design question not a bug that needs diagnosis.

    As part of a theme optimisation process I noticed that the theme’s functions.php file is loaded on every heartbeat ajax call. Now I understand the reason for that IF the theme made use of that trigger. But if the theme does not perform any heartbeat specific functions it seems a waste of server CPU to re-load the active page theme resources on an unrelated AJAX call.

    If not, here then is the code I have added to the theme’s functions.php to ignore the heartbeat:

    if ( isset( $_REQUEST['_nonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_nonce'] ), 'heartbeat-nonce' ) ) {
    	return;
    }

    Is there something I am missing here?

    Thanks in advance for your wisdom…much appreciated ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • If it works, it’s OK. But…

    It may not make a difference to overall speed and server load though. In order to load the functions.php file in your theme, the server first needs to load pretty much all of the WordPress system, and then all of the plugins, and only then it moves onto the theme. Shaving a couple of functions off that won’t make a big difference in any processing time… at best you’d be looking at a millisecond or two. If you’re trying to do some really extreme optimisation where every spare cycle counts, it might be worth looking into, but with the overall complexity of the WordPress system, you wouldn’t be using WordPress if you’re looking for that sort of optimisation level in the first place.

    Thread Starter OnePressTech

    (@timhibberd)

    Cheers @catacaustic. I agree with your assessment. Every little bit helps though ??

    My primary motivation was actually code operation clarity…to clarify all the steps in the theme loading sequence and why. My secondary motivation was to simplify debugging (fewer breakpoint triggers when in xdebug mode).

    Thanks for replying…I wanted to make sure that others were not aware of any side-effects of ignoring heartbeats in the functions.php file. Much appreciated ??

    See if it affects the check for when the user session is expired. The front end only has comments by default, but sites could have other interactive elements for logged in users. If the code you added returns before expected, it might affect plugins or WP uses of the heartbeat.

    Thread Starter OnePressTech

    (@timhibberd)

    Hi Joy…thanks for reaching out to assist. Much appreciated ??

    The theme returning immediately without bothering to load its resources on the server side should not affect client side use of the heartbeat. The response to the heartbeat is actually done by the WordPress core later in the loading sequence after the theme has loaded. And the WordPress loading sequence is sequential so plugins and child themes have already been loaded at this point. Good suggestion though.

    I hypothesise that the core designers provided a heartbeat call to the Theme via functions.php as a standard optional feature for themes to use without having to create their own heartbeat equivalent should they have a need for a heartbeat. If the theme has a use for a heartbeat it hooks the heartbeat and attaches extra data to the heartbeat subsequently returned by the core and / or uses the heartbeat trigger as an event to execute conditional logic.

    I get that.

    But if the theme does not use a heartbeat for any reason I don’t see the value in loading all the theme resources on the server side since they won’t affect the client side.

    My purpose for this post is that I can’t think of a reason a theme’s functions.php should not return immediately on an unused heartbeat and am reaching out to others to verify my assumption.

    You are the first of hopefully many that will lend me sage council. Thanks again ??

    The theme returning immediately without bothering to load its resources on the server side should not affect client side use of the heartbeat. The response to the heartbeat is actually done by the WordPress core later in the loading sequence after the theme has loaded. And the WordPress loading sequence is sequential so plugins and child themes have already been loaded at this point.

    Since you just put one line of code, I couldn’t tell if it was straight in functions.php or when it was executed or what return would actually do in that execution. I was suggesting to determine if that return affects just the theme or the entire AJAX call, because if it affects the entire call, it could be bad.
    Be sure to test the code while in the Customizer, because the theme is involved and the heartbeat is used.

    Thread Starter OnePressTech

    (@timhibberd)

    Thanks @joyously. You are right…a bit more detail is needed.

    The line of code listed previously is executed first in the theme’s functions.php so the theme does nothing else and loads nothing. Functions.php receives the heartbeat and returns immediately.

    There should be no impact on the heartbeat AJAX call itself…there is no expectation by the core heartbeat logic for the theme to do anything with the AJAX call itself.

    I agree with you that the Customizer and the revisions were also top-of-mind with me as possible risk areas for a theme not loading anything on the server side on a heartbeat AJAX-call. Testing has not shown any issues though in the theme suite with the heartbeats ignored.

    NOTE: The theme suite I am modernizing does not currently use the Customizer. I will keep your thoughts in mind though when I upgrade the theme suite to use the Customizer (or wait for the core team to reveal the new Gutenberg Customizer replacement). In particular hooked Customizer callback functions executed by the core on a heartbeat would need to be self-reliant or the theme would need to perform a selective heartbeat-specific loading process.

    Cheers ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Can a theme ignore heartbeats in functions.php without side-effect?’ is closed to new replies.