Wordpress wp_enqueue_scripts not working - css

I have a theme and a child theme and I am trying to load child theme blank css files to override the parent theme files rules.
In parent theme,, there is a style.css in theme root dir and there is responsive.css in /assets/css/ directory
In child theme there is style.css and responsive.css in child root directory
the functions.php has this code in it but the only thing that works is if I apply !important to child style css rules.
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/assets/css/responsive.css' );
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/responsive.css' );
}
How can I get my custom files to load AFTER the them main css files?

You can determine the load order by declaring dependencies. Note that all handles must be unique:
add_action( 'wp_enqueue_scripts', 'so27575579_enqueue_styles' );
function so27575579_enqueue_styles()
{
wp_register_style( 'parent-style', get_template_directory_uri() . '/style.css' );
wp_register_style( 'parent-style-resp', get_template_directory_uri() . '/assets/css/responsive.css', array( 'parent-style' ) );
wp_register_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( 'parent-style-resp' ) );
wp_register_style( 'child-style-resp', get_stylesheet_directory_uri() . '/responsive.css', array( 'child-style' ) );
wp_enqueue_style( 'child-style-resp' );
}

Related

child theme style.css not updating on WordPress | include css from child and parent theme

/include CSS from child and parent theme | style.css not updating on WordPress/
function mychildtheme_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' ); // parent css
wp_enqueue_style( 'custom-style', get_stylesheet_directory_uri() . '/custom-style.css', array(), rand(111,9999), 'all' ); // custom child css
wp_enqueue_style( 'custom-style', get_stylesheet_directory_uri() . '/style.css', array(), rand(111,9999), 'all' ); // child css
}
add_action( 'wp_enqueue_scripts', 'mychildtheme_enqueue_styles' ); // **register hook**
/* here the code for incude the custom CSS in your child theme */
/*include CSS from child and parent theme | style.css not updating on WordPress*/
function mychildtheme_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'custom-style', get_stylesheet_directory_uri() . '/custom-style.css', array(), rand(111,9999), 'all' );
wp_enqueue_style( 'custom-style', get_stylesheet_directory_uri() . '/style.css', array(), rand(111,9999), 'all' );
}
add_action( 'wp_enqueue_scripts', 'mychildtheme_enqueue_styles' ); // register hook

only first line of wp_enqueue_style gets executed

I'm trying to build a child theme for an existing theme someone else built. That theme uses multiple stylesheets and of course, I want the child theme to enqueue them all.
Strangely enough I noticed that that only the first line of the wp_enqueue_style() of the parent theme gets executed while the wp_enqueue_style() of the child theme does work.
I'm kinda stuck and I have no clue on how to make the child theme use all those stylesheets.
function my_theme_enqueue_styles() {
//parent style
$parent_style = 'parent-style';
//enqueue css of the parent theme
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/assets/scripts/css/bootstrap.min.css' );
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/assets/scripts/css/custom-login.css' );
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/assets/scripts/css/font-awesome.min.css' );
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/assets/scripts/css/font-dc-cash.css' );
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/assets/scripts/css/font-dccash.css' );
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/assets/scripts/css/hro-admin.css' );
//enqueue css of the child theme
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style ),
wp_get_theme()->get('Version')
);
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
btw: I know that those css files dont't make any sense. Tell that to the original builder.
download link of the parent theme
The first parameter to wp_enqueue_style() needs to be unique. You are passing in $parent_style for everything. If you give them all unique names, maybe using $parent_style as a prefix, it should fix it.
Give first parameter a name, a unique one so in that way you can enqueue all of them.
Based on wordpress codex:
wp_enqueue_style( string $handle, string $src = '')
$handle
(string) (Required) Name of the stylesheet. Should be unique.
function my_theme_enqueue_styles() {
//parent style
$parent_style = 'parent-style';
//enqueue css of the parent theme
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/assets/scripts/css/bootstrap.min.css' );
wp_enqueue_style( 'custom_login', get_template_directory_uri() . '/assets/scripts/css/custom-login.css' );
wp_enqueue_style( 'font_awesome', get_template_directory_uri() . '/assets/scripts/css/font-awesome.min.css' );
wp_enqueue_style( 'font_dc_cash', get_template_directory_uri() . '/assets/scripts/css/font-dc-cash.css' );
wp_enqueue_style( 'font_dccash', get_template_directory_uri() . '/assets/scripts/css/font-dccash.css' );
wp_enqueue_style( 'hro_admin', get_template_directory_uri() . '/assets/scripts/css/hro-admin.css' );
//enqueue css of the child theme
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( $parent_style ), wp_get_theme()->get('Version') );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

my custom css not loading with `wp_enqueue_script`

I enqueued my custom css in my theme file, but it's not showing any results. When I check the source file i.e. CTRL+U, it's showing my custom-css file, but my CSS file is not working.
function child_theme_scripts_styles() {
wp_enqueue_style( 'Awesome Font', get_stylesheet_directory_uri() . '/css/font-awesome.min.css', array(), '4.3.0' );
wp_enqueue_style('Bootstrap-css', get_stylesheet_directory_uri(). '/css/bootstrap.min.css');
wp_enqueue_script( 'bootstrap-min-js', get_stylesheet_directory_uri() . '/js/bootstrap.min.js', array(), '', true );
wp_enqueue_script( 'custom-css', get_stylesheet_directory_uri() . '/css/custom.css' );
}
add_action( 'wp_enqueue_scripts', 'child_theme_scripts_styles' );
Any help?
you are using wp_enqueue_script to load the stylesheet file. Wordpress couldn't recognize your css file since you are linking with script.
Use this
function child_theme_scripts_styles() {
wp_enqueue_style( 'Awesome Font', get_stylesheet_directory_uri() . '/css/font-awesome.min.css', array(), '4.3.0' );
wp_enqueue_style('Bootstrap-css', get_stylesheet_directory_uri(). '/css/bootstrap.min.css');
wp_enqueue_script( 'bootstrap-min-js', get_stylesheet_directory_uri() . '/js/bootstrap.min.js', array(), '', true );
wp_enqueue_style( 'custom-css', get_stylesheet_directory_uri() . '/css/custom.css' );
}
add_action( 'wp_enqueue_scripts', 'child_theme_scripts_styles' );
Replace
wp_enqueue_script( 'custom-css', get_stylesheet_directory_uri() . '/css/custom.css' );
With
wp_enqueue_style( 'custom-css', get_stylesheet_directory_uri() . '/css/custom.css' );
For Reference Visit
https://codex.wordpress.org/Function_Reference/wp_enqueue_style
https://codex.wordpress.org/Function_Reference/wp_enqueue_script

Wordpress #import or enqueue and does this code make sense?

Just discovered that #import isn't the most efficient way of importing stylesheets experimenting with enqueue. Does this make sense, trying to import several parent stylesheets in various folders:
<?php
/**
* Load the style sheet from the parent theme.
*
*/
function theme_name_parent_styles() {
// Enqueue the parent stylesheet
wp_enqueue_style( 'theme-name-parent-style', get_template_directory_uri() . '/style.css', array(), '0.1', 'all' );
wp_enqueue_style( 'theme-name-parent-style', get_template_directory_uri() . '/css/custom-admin-style.css', array(), '1', 'all' );
wp_enqueue_style( 'theme-name-parent-style', get_template_directory_uri() . '/css/flexslider.css', array(), '1', 'all' );
wp_enqueue_style( 'theme-name-parent-style', get_template_directory_uri() . '/css/thumbfx.css', array(), '1', 'all' );
wp_enqueue_style( 'theme-name-parent-style', get_template_directory_uri() . '/css/dynamic-css/options.css', array(), '1', 'all' );
// Enqueue the parent rtl stylesheet
if ( is_rtl() ) {
wp_enqueue_style( 'theme-name-parent-style-rtl', get_template_directory_uri() . '/rtl.css', array(), '0.1', 'all' );
}
}
add_action( 'wp_enqueue_scripts', 'theme_name_parent_styles' );
?>
First rule: follow the wordpress codex
<?php
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
function enqueue_child_theme_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_uri(), array('parent-style') );
}
Next time I will check the source first.
Solved, thanks for the troubleshoot #m1ro.

Linking wordpress custom bootstrap stylesheet

I'm trying to link my WordPress Stylesheet to child theme at different directory/sub folder, it doesn't seems to work. Below are the code where I place it at functions.php
function theme_add_bootstrap()
{
wp_enqueue_style( 'bootstrap-css', get_template_directory_uri() . 'assets/css/bootstrap.min.css' );
wp_enqueue_style( 'style-css', get_template_directory_uri() . 'assets/css/custom.css' );
wp_enqueue_script( 'bootstrap-js', get_template_directory_uri() . 'assets/js/bootstrap.min.js', array(), '3.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_add_bootstrap' );
I try putting #import statement inside style.css, it doesn't work also.
Use this:-
function theme_add_bootstrap()
{
wp_enqueue_style( 'bootstrap-css', get_template_directory_uri() . '/assets/css/bootstrap.min.css' );
wp_enqueue_style( 'style-css', get_template_directory_uri() . '/assets/css/custom.css' );
wp_enqueue_script( 'bootstrap-js', get_template_directory_uri() . '/assets/js/bootstrap.min.js', array(), '3.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_add_bootstrap' );
Remember that get_template_directory_uri() will give you directory uri path with no /. SO you need to add / after this function.
Hope this will work for you.

Resources