Dashicons are the official icon font used in the WordPress admin dashboard. While they are essential for the backend user interface, they are often enqueued on the frontend of websites, even for visitors who are not logged in. This can lead to unnecessary HTTP requests and slightly increased page load times, which can affect your site's performance metrics and, indirectly, SEO.

Here's how to disable Dashicons CSS and JS files on the frontend for non-logged-in users:

Method 1: Adding Code to Your functions.php File (Recommended)

This is the most common and generally recommended method. You should add this code to your theme's functions.php file or, even better, to a custom plugin for easier management and to prevent losing changes upon theme updates.

PHP
 
<?php
/**
 * Dequeue Dashicons on the front-end for non-logged in users.
 */
function my_deregister_dashicons() {
    if ( ! is_user_logged_in() ) {
        wp_dequeue_style( 'dashicons' );
        wp_deregister_style( 'dashicons' );
    }
}
add_action( 'wp_enqueue_scripts', 'my_deregister_dashicons' );
?>

Explanation:

  • my_deregister_dashicons(): This is the custom function.

  • if ( ! is_user_logged_in() ): This conditional statement ensures that Dashicons are only dequeued for users who are not logged into your WordPress site. Logged-in users (administrators, editors, etc.) will still see them correctly in the admin bar and other backend areas.

  • wp_dequeue_style( 'dashicons' ): This function removes the dashicons stylesheet from the enqueue queue.

  • wp_deregister_style( 'dashicons' ): This function deregisters the dashicons stylesheet entirely, preventing it from being enqueued later by other plugins or themes that might attempt to register it again.

  • add_action( 'wp_enqueue_scripts', 'my_deregister_dashicons' ): This hook ensures your function runs at the appropriate time when scripts and styles are being enqueued.

Method 2: Using a Code Snippet Plugin

If you are not comfortable directly editing your functions.php file, you can use a code snippet plugin like "WPCode" (formerly "WP Code Snippets") or "Code Snippets." These plugins provide a safe interface for adding custom code without modifying core theme files.

  1. Install and activate a code snippet plugin.

  2. Add a new snippet.

  3. Paste the code from Method 1 into the snippet.

  4. Set the type to PHP snippet.

  5. Activate the snippet.

Method 3: Using a Performance Plugin

Many WordPress performance optimisation plugins (e.g., Perfmatters, WP Rocket, Asset CleanUp) include an option to disable Dashicons on the frontend with a simple toggle. This is often the easiest option for users who already utilise such plugins. Look for a "Disable Dashicons" or similar setting within your plugin's performance or asset management sections.

SEO and Performance Impact

Disabling Dashicons on the frontend, especially for non-logged-in users, contributes to:

  • Reduced HTTP Requests: Fewer files loaded means fewer requests the browser needs to make to the server.

  • Smaller Page Size: The dashicons.min.css file, while small, still adds to the overall page weight. Removing it reduces this.

  • Improved Page Load Speed: Both of the above factors contribute to faster page load times.

  • Better Core Web Vitals: Faster loading directly impacts metrics like Largest Contentful Paint (LCP) and First Input Delay (FID), which are crucial for Core Web Vitals and, consequently, SEO.

While the individual impact of removing Dashicons might seem minor, cumulative optimisations significantly improve overall website performance, leading to a better user experience and potentially better search engine rankings. Search engines favour fast-loading websites, so any optimisation that contributes to speed is a positive step for SEO.

Benefit from world-class web hosting, get limitless website design freedom from a web hosting provider with all the great features from Ghana's leading web hosting company. We deliver a powerful and proven platform that is perfect for hosting your websites. See Web Hosting Plans

Ця відповідь Вам допомогла? 0 Користувачі, які знайшли це корисним (0 Голосів)