WordPress.org

Forums

how to remove an action via functions.php in a child theme? (7 posts)

  1. marco-raaphorst
    Member
    Posted 2 years ago #

    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');
  2. chriscarvache
    Member
    Posted 2 years ago #

    remove_action ('wp_print_styles', 'load_fonts');

  3. chriscarvache
    Member
    Posted 2 years ago #

    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...

  4. marco-raaphorst
    Member
    Posted 2 years ago #

    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.

  5. Spark Development
    Member
    Posted 2 years ago #

    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

  6. BjornW
    Member
    Posted 2 years ago #

    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.

  7. marco-raaphorst
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic