0

I'm using a third-party theme with a custom child theme on a website and want to remove the Google Fonts from the parent theme within the child theme's functions.php. This is not working - nothing happens.

/**
 * Remove parent theme Google fonts
 */
function remove_parent_theme_google_fonts() {
    wp_dequeue_style( 'themename_googlefonts-css' );
    wp_deregister_style( 'themename_googlefonts-css' );
}
add_action( 'after_setup_theme', 'remove_parent_theme_google_fonts', 1);

where themename_googlefonts-css is the parent theme's id from the stylesheet link.

Thanks for your help.

EDIT: Removed direct references to specific third-party theme to make this more relevant for other users.

2
  • Are you sure this is the handle? -css suffix is being added automatically by WordPress so try omitting it. also after_setup_theme doesn't seem like the current event to hook into, try wp_enqueue_scripts. You can also play with the priority argument, you have 1 at the moment (default is 10), so try a higher/lower priority. You can also use a plugin called query monitor to check the actual CSS handle name Oct 27, 2022 at 9:07
  • You are right, I accidentally included the default -css suffix. Thanks for the hint! It works with wp_enqueue_scripts. And I will check the helpful plugin you mentioned.
    – winnewoerp
    Oct 28, 2022 at 7:29

2 Answers 2

1

As Buttered_Toast mentioned, I think the hook you want to use is wp_enqueue_scripts rather than after_setup_theme. Changing the priority is also a good idea to make sure your function trigger after the initial call from the parent theme.

So your function would be:

/**
 * Remove Accelerate Google fonts
 */
function remove_accelerate_google_fonts() {
    wp_dequeue_style( 'accelerate_googlefonts-css' );
    wp_deregister_style( 'accelerate_googlefonts-css' );
}
add_action( 'wp_enqueue_scripts', 'remove_accelerate_google_fonts', 50);
1
  • Thank you, it works, except that the handle is not the correct one. See @Buttered_Toast's comment to the question.
    – winnewoerp
    Oct 28, 2022 at 7:30
0

It works with wp_enqueue_scripts and the correct handle without the -css suffix. Here is my fully working solution:

/**
 * Remove parent theme Google fonts
 */
function remove_parent_theme_google_fonts() {
    wp_dequeue_style( 'themename_googlefonts' );
    wp_deregister_style( 'themename_googlefonts' );
}
add_action( 'wp_enqueue_scripts', 'remove_parent_theme_google_fonts', 50);

Not the answer you're looking for? Browse other questions tagged or ask your own question.