• I am using a theme which loads a Google Font via functions.php. in the child theme I want to disable this. how can I do this?

    this is the Google Font code in functions.php :

    function load_fonts() {
              wp_register_style('googleFonts', 'http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic|Oswald');
              wp_enqueue_style( 'googleFonts');
           }
    
    add_action('wp_print_styles', 'load_fonts');
Viewing 6 replies - 1 through 6 (of 6 total)
  • remove_action ('wp_print_styles', 'load_fonts');

    Also… Just an observation…

    Styles shouldn’t be declared in a wp_print_styles action. It should be declared using wp_enqueue_scripts.

    See this article for more…

    Thread Starter marco-raaphorst

    (@marco-raaphorst)

    thanks but doesn’t seem to work. font is loaded. I’ve read that child is read first. so there’s no action to be removed.

    and thanks for the wp_enqueue_scripts suggestion. Waipoua theme of Elma Studio is using that.

    Bit of a cludge, but couldn’t you add an action in the child functions.php to dequeue the style with a lower priority than the original action? E.g.

    function unload_fonts() {
              wp_dequeue_style( 'googleFonts');
           }
    
    add_action('wp_print_styles', 'unload_fonts', 11);

    This is untested btw

    What version of WordPress are you using?

    If I am not mistaken the parent theme should use the following construction to make it easy for child themes to override the parent’s load_fonts function:

    if( ! function_exists('load_fonts') {
    function load_fonts() {
              wp_register_style('googleFonts', 'http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic|Oswald');
              wp_enqueue_style( 'googleFonts');
           }
    add_action('wp_print_styles', 'load_fonts');
    }

    By checking for the existence of the load_fonts function and only defining this function if none is found a parent theme can be prevented from loading it’s own version of the function while allowing a child theme to override it. Since the parent theme isn’t using this construct, the proposed solution from ryanajarret seems to me as a nice hack to unqueue the loading of the fonts without having to change the parent theme.

    Thread Starter marco-raaphorst

    (@marco-raaphorst)

    thanks guys! this works fine. I am using the code of ryanajarrett and the google font is not loaded.

    indeed Bjorn, I don’t want to tweak the original theme.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘how to remove an action via functions.php in a child theme?’ is closed to new replies.