Optimise Your Website via Functions.php File
A better way to approach optimise website via functions.php file is to start with the real-world context rather than a generic opening. This post keeps the focus on scope, assumptions, trade-offs, and the practical decision the reader is trying to make.
Enqueue Scripts and Styles Properly
Use wp_enqueue_script() wp_enqueue_style() and 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');
Optimise Images
Automatically compress and serve optimised 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.
Optimise 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
files and external includes.functions.php
- Merge or Remove Duplicates: Keep only one declaration, preferably in the child theme.
- Use Conditional Checks: Wrap functions with
to prevent redeclaration:function_exists()
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.
Extra context before acting on this topic
optimise website via functions.php file needs enough practical context to stand on its own. Before acting, readers should check the cost, effort, limits, and local suitability behind the idea rather than relying on a broad summary.
