optimize website via functions.php file

Optimize Your Website via Functions.php File

WordPress 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:

Monetize Your Website Instantly

Earn from contextual ads only — simple, fast, and effective.

Earn with Magnet contextual ads Start Earning Now

*Affiliate link – support others and earn rewards

  • Locate Duplicate Declarations: Check both parent and child functions.php files 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.


🔥 View Today’s Top and Popular Deals on Amazon →
Portrait of Patrick Wilson, author and entrepreneur

About the Author

Hello, I'm Patrick Wilson — an entrepreneur, artist, and storyteller driven by curiosity and passion. Through this blog, I explore and share meaningful content around a wide spectrum of lifestyle and success topics that matter to everyday people looking to live better, earn more, and grow intentionally.

From building a personal brand and making money online through proven digital strategies, to navigating the journey of personal finance and wealth-building — I bring real-world insights and tools to help you take control of your financial future.

I also document my pursuit of a healthy, balanced life — sharing inspiration around achieving fitness goals and living with purpose. As someone who appreciates both the aesthetic and the soulful, I dive deep into fine art, cultural history, and the enriching nuances of everyday lifestyle.

Whether I'm exploring breathtaking travel destinations across the globe or tending to the joys of home and garden, I aim to bring beauty, clarity, and useful ideas to every post.

If you're passionate about growth — financially, creatively, or personally — this blog is designed to inspire and support your journey.

Thanks for being here — let's grow together.