How to use parent theme’s enqueue methods

According to the codex, when creating a child theme, you have to manually enqueue the parent theme’s scripts.

My parent theme has logic to conditionally display various css files, cached css, and dynamic css generated from a Theme Options dashboard. The theme is called Camille.

function camille_scripts() {    
    wp_register_style('bootstrap', get_template_directory_uri() . '/css/bootstrap.css');
    wp_enqueue_style( 'bootstrap' );
    //...more scripts & styles
}
function camille_enqueue_dynamic_styles( ) { 
    //renders wp_options to physical file & enqueues physical file
    //...
    wp_register_style( $cache_file_name, $css_cache_file_url, $cache_saved_date);
    wp_enqueue_style( $cache_file_name );
    //...
}

I tried to call these methods in my own theme’s function.php…

function my_theme_scripts() {
    camille_scripts();
    camille_enqueue_dynamic_styles();
}
add_action('wp_enqueue_scripts', 'my_theme_scripts');

But nothing was included. I checked the markup and none of the parent theme CSS was there. Next, I tried copying the full source of these methods into my functions.php and all the styles rendered as usual. However, I dont want to do this because copy-pasting code violates DRY and if the theme is updated, my child theme wont pick up the changes.

Why aren’t my parent theme styles being rendered out to markup? Is there a best-practice for including parent CSS when the parent uses complex logic to produce and enqueue the CSS?

1 Answer
1

If the desired functions are in the parent theme’s functions.php then you can include them in your currently executing child theme functions.php with this call:

include( get_template_directory() . '/functions.php');

get_template_directory() finds the parent’s directory even when using a child theme.

Note that this will run everything found to be executable in the parent theme’s functions.php. So whatever it does, it will do when you pull it in.

If there are things in the parent’s functions.php that are enqueued that you don’t want you can always dequeue them ( after the call to get_template_directory() ) in the child’s functions.php with the appropriate dequeue function: wp_dequeue_script() or wp_dequeue_style()

Leave a Comment