3

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?

0

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()

  • The parent theme's functions.php is being executed after the child theme's functions.php is being executed. Including the parent in the child only causes an error saying the classes/methods are cannot be redeclared. Even if I dont include the parent, I am still able to call the parents methods from the child but the enqueuing simply doesnt have any effect. – Jeff Sep 20 '16 at 17:32
  • 1
    ok, if that's the case then in your child theme functions.php a call like this: add_action('wp_enqueue_scripts', 'camille_enqueue_dynamic_styles'); should work - since that "camille_" function is in scope. – C C Sep 20 '16 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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