Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child theme enqueuing with dependencies

Tags:

php

wordpress

I'm trying to create a child theme. The parent theme has a style.css and all, and I was looking at wp_enqueue_style() function, and it says that you can inlude dependencies. So that means that the themes own style.css can be active, and in my child theme if I specify the same rule in my style.css, it should overwrite it.

But the dependency is an array of handles. How do I find those handles?

wp_enqueue_style( 'mytheme-style', get_stylesheet_directory_uri().'/style.css', array('main_css') );

I tried with the above, but it only loads the style.css from child theme, not the parent.

Where can I find these handles?

EDIT:

I found the code for reproducing the handles and scripts:

function wpa54064_inspect_scripts() {
    global $wp_scripts;
    foreach( $wp_scripts->queue as $handle ) :
        echo $handle,' ';
    endforeach;
}
add_action( 'wp_print_scripts', 'wpa54064_inspect_scripts' );

function wpa54064_inspect_style() {
    global $wp_styles;
    foreach( $wp_styles->queue as $handle ) :
        echo $handle,' ';
    endforeach;
}
add_action( 'wp_print_scripts', 'wpa54064_inspect_style' );

But it still doesn't work the way I thought it does.

like image 791
dingo_d Avatar asked Nov 01 '22 12:11

dingo_d


1 Answers

get_stylesheet_directory_uri() will return the child themes URL if active.

Since you're trying to load the style.css file inside your parent theme you can use get_template_directory_uri() instead.

E.g:

wp_enqueue_style( 'mytheme-style', get_template_directory_uri() . '/style.css', array('main_css') );

My suggestion would be to load your stylesheets like this (code goes inside your child theme's functions.php):

function wpse_load_styles() {
    wp_enqueue_style( 'parent-styles', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'mytheme', get_stylesheet_uri(), array( 'parent-styles' ) );
}
add_action( 'wp_enqueue_scripts', 'wpse_load_styles' );
like image 90
Nathan Dawson Avatar answered Nov 13 '22 04:11

Nathan Dawson