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
}
My Answer:
Your approach of using conditionals in functions.php to optimize the execution of specific functions based on the page type (AMP or non-AMP) is a good idea. By enclosing the functions within conditionals, you are ensuring that unnecessary functions are not executed on every page load, which can help improve performance.
In your case, where you have several functions for AMP pages and several for non-AMP pages, using conditionals can help streamline the execution of these functions based on the page type. This can reduce the overall processing time and improve the efficiency of your website.
As for the performance difference with this amount of code, it may not have a significant impact on Pagespeed Insights metrics. However, every optimization, no matter how small, can contribute to overall performance improvements.
Overall, using conditionals in functions.php to optimize the execution of specific functions based on page type is a good practice and can help improve the performance of your WordPress website.
Rate this post
3 of 5 based on 1977 votesComments