I have this piece of code that registers a CSS in a child theme and when visiting the front-end my_css
is actually included. Why is that ? I thought that registering meant registering for later use
to allow - among other things - conditional enqueuing of stylesheets/scripts. This shouldn't be working without enqueuing my_css
at some point, right ?
function register_my_styles()
{
wp_register_style('parent_theme', get_template_directory_uri() . '/style.css', '', null, 'all');
wp_register_style('my_css', get_stylesheet_directory_uri() . '/src/css/my_css', array('parent_theme'), null, 'all');
}
add_action('wp_loaded', 'register_my_styles');
Commenting out wp_register_style('my_css
at least works :] but the way it behaves prevents me from using conditional enqueuing since my_css
is rendered on the front-end by just using wp_register_style
.
Now the funky part is: I am using ParentTheme 4.10.4 as a parent theme. When I downgrade for each version all the way to 4.0.1 then wp_register_style
works normally and doesn't output the CSS line on the frond-end.
Which would be fine but enqueuing also doesn't work:
function register_my_styles()
{
wp_register_style('parent_theme', get_template_directory_uri() . '/style.css', '', null, 'all');
wp_register_style('my_css', get_stylesheet_directory_uri() . '/src/css/my_css', array('parent_theme'), null, 'all');
}
function enqueue_my_styles()
{
wp_enqueue_style('parent_theme');
wp_enqueue_style('my_css');
}
add_action('wp_loaded', 'register_my_styles');
add_action('wp_enqueue_style', 'enqueue_my_styles');
What am I missing and misunderstanding here ?
EDIT:
Fixing add_action('wp_enqueue_style', 'enqueue_my_styles');
with add_action('wp_enqueue_scripts', 'enqueue_my_styles');
fixed the problem for every version of ParentTheme < 4.10.x. and the rest of functions.php, with conditional enqueuing of styles now work as expected.
So I am inclined to believe there's a problem on the parent theme's side (Divi).
Things were apparently working in some specific conditions for a while despite wp_enqueue_style
not working and that's why it escaped me for some time.
wp_enqueue_scripts
- for enqueuing JS and CSS files.ParentTheme
? Do you have a link? If you've narrowed it down to specific versions what changed in those versions? Did you askParentTheme
support? 3rd party theme dev support is off-topic here. This abnormal behaviour is very likely caused by the parent theme based on what you've said in your question. Sally is right though, these are the incorrect hooks for enqueing scripts and styleswp_enqueue_style
towp_enqueue_scripts
but it's not enough to solve the problem. What works is using a function that do both registering styles and enqueuing styles and put it inwp_enqueue_scripts
hook.