Dev Notes

Various Cheat Sheets and Resources by David Egan/Carawebs.

Deregister Open Sans from WordPress Admin


WordPress, Workflow
David Egan

WordPress enqueues the Open Sans font in the admin area. This requires making a request to Google Fonts, which obviously isn’t going to work if you are offline.

This can be very problematic if you’re developing locally and you lose the internet connection. The local site becomes extremely slow as the request to Google Fonts does it’s thing.

This caught me off guard at first - I blocked my own custom script calls and couldn’t understand what was messing up the local site. It’s WP core calling Google.

The following hack removes Open Sans from the admin area. Add this code to your theme and uncomment the add_action().

Remove Open Sans

<?php
namespace Company\Project\Extras;

/**
 * In case of internet outage
 * @return void
 */
function remove_open_sans() {

    wp_deregister_style( 'open-sans' );
    wp_register_style( 'open-sans', false );
}
// Un-comment these lines in case of internet outage
// Admin area
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\remove_open_sans' );
// Front end - this may be necessary if using default WP themes
add_action('wp_enqueue_scripts', __NAMESPACE__ . '\\remove_open_sans');

If you don’t use namespacing you won’t need __NAMESPACE__ . '\\ in the callback reference.

Seems like a lot of hassle just for a pretty basic admin font. Maybe the enqueuing function could be wrapped in a conditional that pings Google Fonts?

I think core should use system fonts by default - if we need to prettify the admin area, that should be a theme-level decision.

References

The answer by Michael Dance Sort it with a plugin


comments powered by Disqus