optimize website via functions.php file
Optimize Your Website via Functions.php File
Optimizing a website through the functions.php file in WordPress is a common practice to improve performance, functionality, and overall user experience. The functions.php file, part of your theme, allows you to add custom code and functions to your WordPress site. Here are some effective optimization techniques you can implement:
Enqueue Scripts and Styles Properly
Use wp_enqueue_script() and wp_enqueue_style() to load JavaScript and CSS files efficiently. Properly enqueued scripts reduce page load times and prevent conflicts.
function enqueue_custom_scripts() {
wp_enqueue_script(
'custom-script',
get_template_directory_uri() . '/patrick_wilson_cms_js/custom.js',
array('jquery'),
'1.0',
true
);
wp_enqueue_style('custom-style', get_stylesheet_uri());
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');
Enable Gzip Compression
Gzip compression reduces file sizes sent from the server to the browser, improving page load speed.
function enable_gzip_compression() {
if (function_exists('ob_gzhandler')) {
ob_start('ob_gzhandler');
}
}
add_action('init', 'enable_gzip_compression');
Optimize Images
Automatically compress and serve optimized images using plugins like Smush or by adding custom code. Ensure compression does not compromise image quality.
Implement Caching
Caching stores static content to reduce server load and speed up page delivery. Use caching plugins such as W3 Total Cache or WP Super Cache, or implement custom caching solutions via code.
Optimize Database Queries
Reduce the number of database queries and optimise existing ones for better performance. Use the $wpdb object and ensure proper indexing in your queries.
Disable Emojis and Embeds
Remove unnecessary functionality like emojis and oEmbeds to improve load times. Add the following code to functions.php:
function disable_emojis() {
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_styles', 'print_emoji_styles');
remove_filter('the_content_feed', 'wp_staticize_emoji');
remove_filter('comment_text_rss', 'wp_staticize_emoji');
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
}
add_action('init', 'disable_emojis');
Limit Post Revisions
WordPress stores revisions for posts, which can increase database size. Limit the number of revisions or disable them entirely:
define('WP_POST_REVISIONS', 5); // Limit to 5 revisions
Remove Query Strings from Static Resources
Query strings in URLs can prevent proper caching. Remove them using:
if (!function_exists('remove_query_strings')) {
function remove_query_strings() {
// Function code here
}
}
add_action('init', 'remove_query_strings');
Handling “Cannot Redeclare” Errors
This error occurs when a function is declared multiple times. For example, if remove_query_strings() appears in both parent and child themes, it causes a redeclaration error. To fix:
- Locate Duplicate Declarations: Check both parent and child
functions.phpfiles and external includes. - Merge or Remove Duplicates: Keep only one declaration, preferably in the child theme.
- Use Conditional Checks: Wrap functions with
function_exists()to prevent redeclaration:
if (!function_exists('remove_query_strings')) {
function remove_query_strings() {
// Function code here
}
}
- Clear Cache: After changes, clear browser cache and caching plugins, then reload the site.
Always back up your functions.php file before making changes and test on a staging or local environment to prevent site errors or downtime.
Traffic Coop Earnings
Ready to Monetise Your Traffic?
Stop letting your visitors slip away without value. With the LeadsLeap Co-op, you can turn every click into income. Join through my link below and I’ll personally share my tips for getting started fast.
Join My LeadsLeap Co-op Now