• Am I thinking right that several functions that are specific to WP page types would be more optimally enclosed in a conditional in functions.php, instead of the server having to evaluate several functions for every page that end up only doing something for another page type via a conditional inside the functions? Specifically I have several functions for AMP pages and several for non-AMP pages.

    I never see this suggested. Am I missing something on why this would not be optimal? Or would the performance difference with this amount of code be so tiny that it wouldn’t register in any Pagespeed Insights metric?

    My functions.php is 12KB, and an include file I need for AMP pages is 57KB.

    Here’s what I have in mind:

    if (function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint()) {
        
        // Page is AMP. Apply several filters, actions, and an include:
        
        include('simple_html_dom.php');
        add_filter( 'the_content', 'amp_filter' );
        function amp_filter( $content ) { /* code */ }
    
        add_action('avada_before_body_content','gtm_body_code', 1);
        function gtm_body_code() { /* code */ }
    
        // etc
    } else {
        // several non-AMP functions here
    }
Viewing 1 replies (of 1 total)
  • Moderator Imagebcworkz

    (@bcworkz)

    AFAIK, optimizing for AMP has not been a WP development goal. There is a lot about WP that is not optimal for AMP. Ideally, WP for AMP would use an entirely different paradigm.

    As for functions.php specifically, I’d expect little if any gain from conditional loading. While loading unneeded functions does consume resource, so does evaluation of conditionals. Depending on their nature, some conditionals could make things worse instead of better. TBH, I’ve not seen any real world statistics regarding performance one way or the other. I’m merely speculating. Any difference would likely be on the microtimer level, too small a difference to be noticeable to end users.

Viewing 1 replies (of 1 total)

The topic ‘Optimizing functions.php’ is closed to new replies.