I'm trying to create a Wordpress child theme, but I can't seem to get it to override styles in the parent theme.
functions.php
<?php
if ( ! function_exists( 'my_theme_enqueue_styles' ) ) {
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
}
/**
* WP Enqueue Stylesheets
*/
if ( ! function_exists( 'rev_cust_enqueue_stylesheets' ) ) {
add_action( 'wp_enqueue_scripts', 'rev_cust_enqueue_stylesheets' );
function rev_cust_enqueue_stylesheets() {
wp_enqueue_style( 'rev-main', get_template_directory_uri() . '/layout/css/rev_main.css', array(), '', 'all' );
}
}
?>
rev_main.css
#header .top-header .header-navigation ul li {
margin-left: 40px;
line-height: 1.375;
font-weight: normal !important;
font-size: 1px;
color: #fff;
text-transform: uppercase;
position: relative;
float: left;
}
Why isn't my style class not being picked?
Thank you all in advance.
UPDATE
I don't think the css file even gets picked up,
I have the child theme enabled.
The problem was at:
get_template_directory_uri()
in:
/**
* WP Enqueue Stylesheets
*/
if ( ! function_exists( 'rev_cust_enqueue_stylesheets' ) ) {
add_action( 'wp_enqueue_scripts', 'rev_cust_enqueue_stylesheets' );
function rev_cust_enqueue_stylesheets() {
wp_enqueue_style( 'rev-main', get_template_directory_uri() . '/layout/css/rev_main.css', array(), '', 'all' );
}
}
?>
This points to the parent theme, instead of the child theme.
It should be get_stylesheet_directory_uri as follows:
function rev_cust_enqueue_stylesheets() {
wp_enqueue_style( 'rev-main', get_stylesheet_directory_uri() . '/layout/css/rev_main.css', array(), '', 'all' );
}
Related
Woocommerce injects the following inline css rule into the head section of my theme. Any idea how to remove it through my child themes functions.php?
<style id='woocommerce-inline-inline-css' type='text/css'>.woocommerce form .form-row .required { visibility: visible; }</style>
If im not missing anything the following code block in the woocommerce plugins file ...woocommerce-includes/class-wc-frontend-scripts.php is responsible for it.
// Placeholder style.
wp_register_style( 'woocommerce-inline', false ); // phpcs:ignore
wp_enqueue_style( 'woocommerce-inline' );
if ( true === wc_string_to_bool( get_option( 'woocommerce_checkout_highlight_required_fields', 'yes' ) ) ) {
wp_add_inline_style( 'woocommerce-inline', '.woocommerce form .form-row .required { visibility: visible; }' );
} else {
wp_add_inline_style( 'woocommerce-inline', '.woocommerce form .form-row .required { visibility: hidden; }' );
}
The following action removes
<style id='woocommerce-inline-inline-css' type='text/css'>.woocommerce form .form-row .required { visibility: visible; }</style>
from the head section. Code goes into your functions.php file of your child or parent theme.
Input from O. Jones in the comments: Or, consider using the Code Snippets plugin to hold these small tweaks to a site. If you edit functions.php you may (a) have it overwritten by an update, (b) possibly lose track of where you put your tweaks.
// Remove the following from head section - see source code
// <style id='woocommerce-inline-inline-css' type='text/css'>.woocommerce form .form-row .required { visibility: visible; }</style>
add_action('wp_enqueue_scripts', 'remove_woo_inline_css_head_ac',11);
function remove_woo_inline_css_head_ac() {
wp_deregister_style( 'woocommerce-inline' );
}
Just add // before function to comment that line
This is the code correctly:
// Placeholder style.
wp_register_style( 'woocommerce-inline', false ); // phpcs:ignore
wp_enqueue_style( 'woocommerce-inline' );
if ( true === wc_string_to_bool( get_option( 'woocommerce_checkout_highlight_required_fields', 'yes' ) ) ) {
//wp_add_inline_style( 'woocommerce-inline', '.woocommerce form .form-row .required { visibility: visible; }' );
} else {
//wp_add_inline_style( 'woocommerce-inline', '.woocommerce form .form-row .required { visibility: hidden; }' );
}
This is what bothers me.
This is my functions.php:
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array( 'avada-stylesheet','avada- max-2c','avada-min-2c-max-3c','avada-min-3c-max-4c','avada-min-4c-max-5c','avada-min-5c-max-6c','avada-min-768-max-1024-p','avada-min-768-max-1024-l','avada-max-shbp-18','avada-max-shbp-32','avada-max-640','avada-min-768-max-1024-woo' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
if ( !function_exists( 'child_theme_configurator_css' ) ):
function child_theme_configurator_css() {
wp_enqueue_style( 'chld_thm_cfg_child', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', array( 'chld_thm_cfg_parent' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css', 20 );
// END ENQUEUE PARENT ACTION
An this is my style.css file in my child theme:
/*
Theme Name: Avada Child
Theme URI: http://avada.theme-fusion.com/
Template: Avada
Author: ThemeFusion
Author URI: http://themeforest.net/user/ThemeFusion
Description: The #1 selling theme of all time that allows you to build virtually any design style.
Version: 5.7.2.1545428017
Updated: 2018-12-21 22:33:37
*/
.woocommerce-message {
padding-left: 18px;
background-color: #252a33;
border-top: 1px solid rgba(128,128,128,1);
border-top-color: rgb(128, 128, 128);
border-bottom: 1px solid rgba(128,128,128,1);
border-bottom-color: rgb(128, 128, 128);
color: rgba(128,128,128,1);
}
My goal is to change background-color, but it doesn't work ... I used Child Theme Configurator plugin to make a child theme, and I think that plugin is responsible for that how function.php looks like...
Please help.
Paul
I want the modified child theme site-banner1.css stylesheet to load after the parent theme so it overwrites the parent file with the changes. I am using the enqueue array() to try and push site-banner1.css to the end of the queue by inputting the last style id (CSS from a plugin) into the array function. Here is the code from functions.php:
<?php
if ( !defined( 'ABSPATH' ) ) exit;
if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit(
get_template_directory_uri() ) . 'style.css', array( ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
function my_theme_enqueue_styles() {
$parent_style = 'tesseract-site-banner';
wp_enqueue_style( $parent_style,
get_template_directory_uri() . '/css/site-banner.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/site-banner1.css',
array( $parent_style, 'tt-easy-google-font-styles')
); }
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles',99999 );
Currently site-banner1.css is visible in the source code but it is not the last stylesheet to load and so the changes are not visible. The parent site-banner.css is still visible in the dev tools with the styles. By adding the last CSS id into the array can I force site-banner1.css to load after it, I think code is missing from the array but not sure what? If this is incorrect what is the way to force the sheet to load at the end of the queue?
I managed to fix the problem using wp_deregister_style:
<?php
if ( !defined( 'ABSPATH' ) ) exit;
if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit(
get_template_directory_uri() ) . 'style.css', array( ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
add_action( 'wp_enqueue_scripts', 'load_alta_styles', 200 );
function load_alta_styles() {
wp_dequeue_style( 'tesseract-site-banner' );
wp_deregister_style( 'tesseract-site-banner' );
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() .
'/site-banner1.css' );
}
I've searched through several questions and still having trouble. I'd really appreciate some expertise from y'all. =)
The child theme created and activated on my WordPress site seems to be enacting no changes.
I know essentially zero PHP but trying to use the functions.php instead of #import since it's supposed to be a better load time.
Here's what I have in my child theme's directory style.css (first) and functions.php (second)
/*
Theme Name: Quest-Child
Description: No Coward's Quest Child Theme
Author: Jason from No Coward
Author URI: http://www.nocoward.com
Template: quest
Version: 1.0
*/
/* add padding to site description */
.site-description {
padding-top: 15px;
padding-bottom: 30px;
}
.body {
background: red;
}
/* can be found on "Services" page. Is the first ID within the <p> element */
#cc-m-12571703896 {
background: blue;
}
<?php
function my_theme_enqueue_styles() {
$parent_style = 'quest-all-css'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
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' );
?>
WordPress recognizes the child theme and has allowed me to activate it successfully.
The stuff in CSS is what I've tried to use for testing my child-theme laying down my changes. Nothing is appearing.
Again, I have zero PHP knowledge. Based on the official WordPress "How to create a child theme", I copied and pasted the code above and tried to fill in the "parent-style" value based on their instructions.
Here's what I saw on my parent theme I could possibly be using for the functions.php "parent-style"...
// Enqueue required styles
wp_enqueue_style( 'quest-bootstrap', get_template_directory_uri() . '/assets/plugins/bootstrap/css/bootstrap.min.css' );
wp_enqueue_style( 'smartmenus', get_template_directory_uri() . '/assets/plugins/smartmenus/addons/bootstrap/jquery.smartmenus.bootstrap.css' );
wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/assets/plugins/font-awesome/css/font-awesome.min.css' );
wp_enqueue_style( 'animate-css', get_template_directory_uri() . '/assets/plugins/animate/animate.css' );
wp_enqueue_style( 'slit-slider', get_template_directory_uri() . '/assets/plugins/FullscreenSlitSlider/css/style.css' );
wp_enqueue_style( 'colorbox', get_template_directory_uri() . '/assets/plugins/colorbox/colorbox.css' );
wp_enqueue_style( 'Quest-style', get_stylesheet_uri(), array(
'quest-bootstrap',
'smartmenus',
'font-awesome',
'animate-css',
'slit-slider',
'colorbox'
) );
...
// Enqueue required styles
wp_enqueue_style( 'quest-all-css', get_template_directory_uri() . '/assets/css/plugins-all.min.css' );
wp_enqueue_style( 'Quest-style', get_stylesheet_uri(), array( 'quest-all-css' ) );
// Enqueue required scripts
wp_enqueue_script( 'quest-all-js', get_template_directory_uri() . '/assets/js/quest-and-plugins.js', array(
'jquery',
'masonry'
) );
What am I missing?
Thanks in advance!
-Jason
I managed to fix this. Here's the PHP code I found on the help forum. I think the "Portfolio" bit may be extraneous, but it's working so I'm leaving it there just in case.
<?php
add_action( 'wp_enqueue_scripts', 'quest_child_enqueue_styles' );
function quest_child_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
add_filter( 'quest_plus_template_content-portfolio', 'quest_child_template_portfolio' );
function quest_child_template_portfolio(){
return get_stylesheet_directory() . '/content-portfolio.php';
}
Caveat to this solution: There is a weird thing happening. I put in some styling to make something "Background: red" so I could test if the child theme was working, and despite deleting that bit of code, it is still appearing.
For now I'm chalking it up to slow hosting but be aware that's happening if you follow this solution.
Hope this helps someone else! =)
Jason
In my Wordpress theme I've been using an enqueue script function that adds my jQuery scripts and my stylesheets to my site. Because I have a separate page template as well as my standard page.php , I've been using the 'if' and 'else' commands as written in the code below, to differentiate between the two stylesheets.
<?php
/* ENQUEUE SCRIPTS AND STYLESHEETS ----------- */
function mywebsite_scripts() {
/* JAVASCRIPT ----------- */
wp_enqueue_script(
'mywebsite_scripts-menu-toggle',
get_template_directory_uri() . '/js/menu-toggle.js' );
wp_enqueue_script(
'mywebsite_scripts-add-submenu',
get_template_directory_uri() . '/js/add-submenu.js' );
wp_enqueue_script(
'mywebsite_scripts-upanddown',
get_template_directory_uri() . '/js/upanddown.js' );
/* END JAVASCRIPT ----------- */
/* PAGE TEMPLATE STYLESHEETS ----------- */
if (is_page_template('page-templates/full-page.php'))
{ wp_enqueue_style( 'mywebsite_scripts-full-page' ,
get_template_directory_uri() . '/css/full-page.css'); }
/* END PAGE TEMPLATE STYLESHEETS ----------- */
/* MAIN STYLESHEET ----------- */
else { wp_enqueue_style( 'mywebsite_scripts_style', get_stylesheet_uri() ); } }
/* END MAIN STYLESHEET ----------- */
add_action( 'wp_enqueue_scripts', 'mywebsite_scripts_scripts' );
/* END ENQUEUE SCRIPTS AND STYLESHEETS ----------- */
When I began attempting to make my theme compatible with earlier versions of Internet Explorer, I followed this tutorial: https://gist.github.com/wpscholar/4947518
This code works when I add it to my functions folder, but doesn't allow for the javascript enqueues or extra page template stylesheets that I need.
<?php
function enqueue_my_styles()
{
global $wp_styles;
// LOAD THE MAIN STYLESHEET
wp_enqueue_style('mywebsite_scripts_style', get_stylesheet_uri());
wp_enqueue_style('mywebsite_scripts_style_ie8', get_stylesheet_directory_uri() . '/css/ie8.css', array(
'mywebsite_scripts_style'
));
$wp_styles->add_data('mywebsite_scripts_style_ie8', 'conditional', 'IE 8');
wp_enqueue_style('mywebsite_scripts_style_ie9', get_stylesheet_directory_uri() . '/css/ie9.css', array(
'mywebsite_scripts_style'
));
$wp_styles->add_data('mywebsite_scripts_style_ie9', 'conditional', 'IE 9');
}
add_action('wp_enqueue_scripts', 'enqueue_my_styles');
I've tried to combine the two codes and ended up with the code below. Although it hasn't given me any PHP errors, it doesn't load the I.E stylesheets, and simply seems to revert back to the very first code above. Can anyone help me by pointing out what I've done wrong? What I wanted to achieve by combining these codes is the ability to add stylesheets for multiple page templates, and also have the I.E dependent stylesheets that inherit the styles from my style.css and only overwrite the necessary CSS rules to work on older browsers. I also need my Javascript to work too.
I hope all this is possible, any help would be much appreciated as I've tried my best to make it work and I'm not sure where I'm going wrong.
/* ENQUEUE SCRIPTS AND STYLESHEETS ----------- */
function mywebsite_scripts_scripts() {
/* JAVASCRIPT ----------- */
wp_enqueue_script(
'mywebsite_scripts-menu-toggle',
get_template_directory_uri() . '/js/menu-toggle.js' );
wp_enqueue_script(
'mywebsite_scripts-add-submenu',
get_template_directory_uri() . '/js/add-submenu.js' );
wp_enqueue_script(
'mywebsite_scripts-upanddown',
get_template_directory_uri() . '/js/upanddown.js' );
/* END JAVASCRIPT ----------- */
/* PAGE TEMPLATE STYLESHEETS ----------- */
if (is_page_template('page-templates/full-page.php'))
{ wp_enqueue_style( 'mywebsite_scripts-full-page' ,
get_template_directory_uri() . '/css/full-page.css');
wp_enqueue_style( 'mywebsite_scripts-full-page_ie', get_stylesheet_directory_uri() . '/css/full-page-ie8.css',
array( 'mywebsite_scripts-full-page' ) ); $wp_styles->add_data( 'mywebsite_scripts-full-page_ie', 'conditional', 'IE 8' );
}
/* END PAGE TEMPLATE STYLESHEETS ----------- */
/* MAIN STYLESHEET ----------- */
else { wp_enqueue_style( 'mywebsite_scripts_style', get_stylesheet_uri() );
wp_enqueue_style( 'mywebsite_scripts_style_ie8’, get_stylesheet_directory_uri() . '/css/ie8.css',
array( 'mywebsite_scripts_style' ) ); $wp_styles->add_data( 'mywebsite_scripts_style_ie8’, 'conditional', 'IE 8' );
} }
/* END MAIN STYLESHEET ----------- */
add_action( 'wp_enqueue_scripts', 'mywebsite_scripts_scripts' );
/* END ENQUEUE SCRIPTS AND STYLESHEETS ----------- */
*** UPDATE
<?php
/* ENQUEUE SCRIPTS AND STYLESHEETS ----------- */
function mywebsite_scripts_scripts() {
/* JAVASCRIPT ----------- */
wp_enqueue_script(
'mywebsite_scripts-menu-toggle',
get_template_directory_uri() . '/js/menu-toggle.js' );
wp_enqueue_script(
'mywebsite_scripts-add-submenu',
get_template_directory_uri() . '/js/add-submenu.js' );
wp_enqueue_script(
'mywebsite_scripts-upanddown',
get_template_directory_uri() . '/js/upanddown.js' );
/* END JAVASCRIPT ----------- */
if (is_page_template('page-templates/full-page.php'))
{ wp_enqueue_style( 'mywebsite_scripts-full-page' ,
get_template_directory_uri() . '/css/full-page.css');
}
else { wp_enqueue_style( 'mywebsite_scripts_style', get_stylesheet_uri() );
wp_enqueue_style( 'mywebsite_scripts_style_ie9', get_stylesheet_directory_uri() . '/css/ie9.css',
array( 'mywebsite_scripts_style' ) );
wp_style_add_data( 'mywebsite_scripts-style_ie9', 'conditional', 'IE 9' );
} }
add_action( 'wp_enqueue_scripts', 'mywebsite_scripts_scripts' );
*** UPDATE
Ok I've managed to get this to work, but only with the first method using global styles. I'm still not sure why the updated version isn't working.
/* ENQUEUE SCRIPTS AND STYLESHEETS ----------- */
function mywebsite_scripts_scripts() {
/* JAVASCRIPT ----------- */
wp_enqueue_script(
'mywebsite_scripts-menu-toggle',
get_template_directory_uri() . '/js/menu-toggle.js' );
wp_enqueue_script(
'mywebsite_scripts-add-submenu',
get_template_directory_uri() . '/js/add-submenu.js' );
wp_enqueue_script(
'mywebsite_scripts-upanddown',
get_template_directory_uri() . '/js/upanddown.js' );
/* END JAVASCRIPT ----------- */
/* PAGE TEMPLATE STYLESHEETS ----------- */
global $wp_styles;
if (is_page_template('page-templates/full-page.php'))
{ wp_enqueue_style( 'mywebsite_scripts-full-page' ,
get_template_directory_uri() . '/css/full-page.css');
}
/* END PAGE TEMPLATE STYLESHEETS ----------- */
/* MAIN STYLESHEET ----------- */
else { wp_enqueue_style( 'mywebsite_scripts_style', get_stylesheet_uri() );
wp_enqueue_style( 'mywebsite_scripts_style_ie8', get_stylesheet_directory_uri() . '/css/ie8.css', array( 'mywebsite_scripts_style' ) );
$wp_styles->add_data( 'mywebsite_scripts_style_ie8', 'conditional', 'IE 8' );
wp_enqueue_style( 'mywebsite_scripts_style_ie9', get_stylesheet_directory_uri() . '/css/ie9.css', array( 'mywebsite_scripts_style' ) );
$wp_styles->add_data( 'mywebsite_scripts_style_ie9', 'conditional', 'IE 9' );
} }
/* END MAIN STYLESHEET ----------- */
add_action( 'wp_enqueue_scripts', 'mywebsite_scripts_scripts' );
/* END ENQUEUE SCRIPTS AND STYLESHEETS ----------- */
FOR IE CONDITIONAL STYLE SHEETS
After you enqueue your scripts, You can use the following:
wp_style_add_data( 'ie-theme', 'conditional', 'IE' );
So for the example above, lets say you have a script that gets enqueued like the following.
wp_enqueue_style( 'ie-theme', get_stylesheet_directory_uri() . "/css/ie.css" );
You would add the above after you enqueue ie-theme making the full declaration appear:
wp_enqueue_style( 'ie-theme', get_stylesheet_directory_uri() . "/css/ie.css" );
wp_style_add_data( 'ie-theme', 'conditional', 'IE' );
It would appear that you read the top part of the gist but did not go any lower. As of a recent version of WordPress, there has been an update where you should use wp_style_add_data() function for this instead of the $wp_styles->add_data(); example
FOR TEMPLATE SPECIFIC STYLESHEETS
To enqueue stylesheets for a single template, you would wrap that wp_enqueue_style method in a check
if ( is_page( 'landing-page-template-one' ) ) {
/** Call landing-page-template-one enqueue */
}
See This Stack Exchange question for reference