I am using wordpress 4.9.5 and I am en-queuing in my functions.php my styles as the following:
function enqueue_parent_theme_style()
{
if ( is_page( 'product builder' ) || is_page('shopping products')) {
$parentStyle = 'parent-style';
//css
wp_enqueue_style($parentStyle, get_template_directory_uri() . '/style.css');
wp_enqueue_style('bootstrap-4.0.0', get_stylesheet_directory_uri() . '/css/bootstrap.min.css', array($parentStyle));
wp_enqueue_style('dataTables', '//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css', array($parentStyle) );
wp_enqueue_style('dataTables-1.10.16', get_stylesheet_directory_uri() . '/css/dataTables.bootstrap4.min.css', array($parentStyle));
//js
wp_enqueue_script('font-awesome', 'https://use.fontawesome.com/releases/v5.0.10/js/all.js', NULL, '1.0', true);
wp_enqueue_script('main-shopping-product-js', get_theme_file_uri('/js/scripts-bundled.js'), NULL, '1.0', true);
wp_localize_script('main-shopping-product-js', 'shoppingproductData', array(
'root_url' => get_site_url(),
'nonce' => wp_create_nonce('wp_rest')
));
}
}
add_action('wp_enqueue_scripts', 'enqueue_parent_theme_style');
As you can see I only want to load my child css/javascript on two child pages, product builder and shopping products. My css/javascript works on these two pages:
On my two child pages the theme's css and javascript is working perfectly fine. However, when I go back to my main theme pages, the css and javascript is broken.
The pages look like the following:
Any suggestions what I am doing wrong?
Update
My browser console shows the following:
The problem was with the style.css, which was in the if condition:
if ( is_page( 'product builder' ) || is_page('shopping products')) {
$parentStyle = 'parent-style';
//css
wp_enqueue_style($parentStyle, get_template_directory_uri() . '/style.css');
The style.css file contains all css rules( menu part, too ), which construct the pages layouts. Moving out from condition that part will load style.css file in all pages:
function enqueue_parent_theme_style()
{
$parentStyle = 'parent-style';
//css
wp_enqueue_style($parentStyle, get_template_directory_uri() . '/style.css');
if (is_page('product builder') || is_page('shopping products')) {
wp_enqueue_style('bootstrap-4.0.0', get_stylesheet_directory_uri() . '/css/bootstrap.min.css', array($parentStyle));
wp_enqueue_style('dataTables', '//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css', array($parentStyle));
wp_enqueue_style('dataTables-1.10.16', get_stylesheet_directory_uri() . '/css/dataTables.bootstrap4.min.css', array($parentStyle));
//js
wp_enqueue_script('font-awesome', 'https://use.fontawesome.com/releases/v5.0.10/js/all.js', NULL, '1.0', true);
wp_enqueue_script('main-shopping-product-js', get_theme_file_uri('/js/scripts-bundled.js'), NULL, '1.0', true);
wp_localize_script('main-shopping-product-js', 'shoppingproductData', array(
'root_url' => get_site_url(),
'nonce' => wp_create_nonce('wp_rest')
));
}
}
add_action('wp_enqueue_scripts', 'enqueue_parent_theme_style');
Related
I want to load bundle.min.css only in contact.php. Is there any solution so it effect only contact.php with other css. And i wanna exclude bundle.min.css from other pages.
<?php
function tex_enqueue() {
wp_enqueue_style( 'googleapi', 'https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700' );
wp_enqueue_style( 'bootstrap-s', get_template_directory_uri() . '/css/bootstrap.css' );
wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/icon-fonts/font-awesome-4.5.0/css/font-awesome.min.css' );
wp_enqueue_style( 'essential-regular-fonts', get_template_directory_uri() . '/icon-fonts/essential-regular-fonts/essential-icons.css' );
wp_enqueue_style( 'reset', get_template_directory_uri() . '/css/reset.css' );
wp_enqueue_style( 'magnific-popup', get_template_directory_uri() . '/css/magnific-popup.css' );
wp_enqueue_style( 'common-css', get_template_directory_uri() . '/css/common.css', ) );
wp_enqueue_style( 'myncss-css', get_template_directory_uri() . '/css/myncss.css' );
wp_enqueue_style( 'bundle-css', get_template_directory_uri() . '/css/bundle.min.css' );
wp_register_style( 'animate-css',get_template_directory_uri() . '/css/animate.css',array(),'1.1','all');
wp_enqueue_style( 'animate-css');
wp_enqueue_style( 'owlcarousel-style', get_template_directory_uri() . '/owl/owl.carousel.css' );
wp_enqueue_style( 'portfolio-css', get_template_directory_uri() . '/css/portfolio.css' );
wp_enqueue_style( 'style', get_stylesheet_uri() );
}
add_action('wp_enqueue_scripts', 'tex_enqueue');
?>
You can add your css on specific page by using is_page('your_page_id') condition tag.
Create page template for your contact.php, See this link : Click Here
Use the below code in your current active theme functions.php file.
function tex_enqueue() {
if(is_page('your-contact-page-id')){
wp_enqueue_style( 'bundle-css', get_template_directory_uri() . '/css/bundle.min.css' );
}
}
add_action('wp_enqueue_scripts', 'tex_enqueue');
For more help see this link : click here
I'am trying to include all my css and JS files of a theme using functions.php
Folowing is what i have done so far
<?php
function blogroom() {
wp_enqueue_style('bootstrap', get_stylesheet_directory_uri() . '/assets/lib/bootstrap/dist/css/bootstrap.min.css');
wp_enqueue_style('loaders', get_stylesheet_directory_uri() . '/assets/lib/loaders.css/loaders.min.css');
wp_enqueue_style('iconsmind', get_stylesheet_directory_uri() . '/assets/lib/iconsmind/iconsmind.css');
wp_enqueue_style('hamburgers', get_stylesheet_directory_uri() . '/assets/lib/hamburgers/dist/hamburgers.min.css');
wp_enqueue_style('font-awesome-css', get_stylesheet_directory_uri() . '/assets/lib/font-awesome/css/font-awesome.min.css');
wp_enqueue_style('theme-style', get_stylesheet_directory_uri() . '/assets/css/style.css');
wp_enqueue_style('theme-style', get_stylesheet_directory_uri() . '/assets/css/custom.css');
wp_register_script( 'bootstrap-js', get_template_directory_uri() . '/assets/lib/bootstrap/dist/js/bootstrap.min.js');
wp_register_script( 'imageloaded', get_template_directory_uri() . '/assets/lib/imagesloaded/imagesloaded.pkgd.min.js', array( 'bootstrap-js' ) );
wp_register_script( 'tweenmax', get_template_directory_uri() . '/assets/lib/gsap/src/minified/TweenMax.min.js', array('imageloaded') );
wp_register_script( 'scroll-to-plugin', get_template_directory_uri() . '/assets/lib/gsap/src/minified/plugins/ScrollToPlugin.min.js', array('tweenmax') );
wp_register_script( 'customToEase', get_template_directory_uri() . '/assets/lib/CustomEase.min.js', array('scroll-to-plugin') );
wp_register_script( 'configJs', get_template_directory_uri() . '/assets/js/config.js', array('customToEase') );
wp_register_script( 'zanimation', get_template_directory_uri() . '/assets/js/zanimation.js', array('configJs') );
wp_register_script( 'corejs', get_template_directory_uri() . '/assets/js/core.js', array('zanimation') );
wp_register_script( 'mainjs', get_template_directory_uri() . '/assets/js/main.js', array('corejs') );
wp_enqueue_script( 'mainjs' );
}
add_action( 'wp_enqueue_scripts', 'blogroom' );
?>
Here, this only loads my CSS file and not my js files. not a single javascript file is loaded.
Can someone please help?
You are only registering the scripts without enqueuing them.
Do for each script the same as you did for main.js, meaning for each registered script, enqueue it
wp_enqueue_script( 'your registered script name' );
Also make sure you have wp_head() and wp_footer() in your theme header and, respectively the footer.
I think this may be due to a misunderstanding of wp_register_script vs wp_enqueue_script.
wp_register_script only tells WP that there is a script at the given location, and gives it a "handle" to be used for other purposes.
wp_enqueue_scripts tells WP to use the script. And, there's two formats - the "long" format is like wp_register_script, so you can tell WP where the script is, AND to load it, all in one command.
The code below will work - I've changed your wp_register_script calls to wp_enqueue_script:
function blogroom() {
wp_enqueue_style('bootstrap', get_stylesheet_directory_uri() . '/assets/lib/bootstrap/dist/css/bootstrap.min.css');
wp_enqueue_style('loaders', get_stylesheet_directory_uri() . '/assets/lib/loaders.css/loaders.min.css');
wp_enqueue_style('iconsmind', get_stylesheet_directory_uri() . '/assets/lib/iconsmind/iconsmind.css');
wp_enqueue_style('hamburgers', get_stylesheet_directory_uri() . '/assets/lib/hamburgers/dist/hamburgers.min.css');
wp_enqueue_style('font-awesome-css', get_stylesheet_directory_uri() . '/assets/lib/font-awesome/css/font-awesome.min.css');
wp_enqueue_style('theme-style', get_stylesheet_directory_uri() . '/assets/css/style.css');
wp_enqueue_style('theme-style', get_stylesheet_directory_uri() . '/assets/css/custom.css');
wp_enqueue_script( 'bootstrap-js', get_template_directory_uri() . '/assets/lib/bootstrap/dist/js/bootstrap.min.js');
wp_enqueue_script( 'imageloaded', get_template_directory_uri() . '/assets/lib/imagesloaded/imagesloaded.pkgd.min.js', array( 'bootstrap-js' ) );
wp_enqueue_script( 'tweenmax', get_template_directory_uri() . '/assets/lib/gsap/src/minified/TweenMax.min.js', array('imageloaded') );
wp_enqueue_script( 'scroll-to-plugin', get_template_directory_uri() . '/assets/lib/gsap/src/minified/plugins/ScrollToPlugin.min.js', array('tweenmax') );
wp_enqueue_script( 'customToEase', get_template_directory_uri() . '/assets/lib/CustomEase.min.js', array('scroll-to-plugin') );
wp_enqueue_script( 'configJs', get_template_directory_uri() . '/assets/js/config.js', array('customToEase') );
wp_enqueue_script( 'zanimation', get_template_directory_uri() . '/assets/js/zanimation.js', array('configJs') );
wp_enqueue_script( 'corejs', get_template_directory_uri() . '/assets/js/core.js', array('zanimation') );
wp_enqueue_script( 'mainjs', get_template_directory_uri() . '/assets/js/main.js', array('corejs') );
}
add_action( 'wp_enqueue_scripts', 'blogroom' );
Typically, you would only use wp_register_script in a few scenarios - here's two common ones:
1. You want to localize_script using wp_localize_script (this permits you to output javascript variables from your PHP that would be considered "related" to the registered script)
2. You want to register the script because you may or may NOT output the scripts later (this is particularly useful when authoring a shortcode, you can register the script, set a "flag" if the shortcode is rendered, and the footer can output the scripts only if the flag is set). This method can be seen in this article: How to load JavaScript like a WordPress Master
I have a problem: the css and js files not loaded, but It's showed on source code.
I'm including it on functions.php file just like below code.
The source page: http://jalid-wp.co.nf
Please any help for this problem.
function load_scripts() {
// CSS Files
wp_enqueue_style( 'main', get_template_directory_uri() . '/assets/css/main.css' );
wp_enqueue_style( 'responsive', get_template_directory_uri() . '/assets/css/responsive.css' );
wp_enqueue_style( 'animate', get_template_directory_uri() . '/assets/css/animate.css' );
wp_enqueue_style( 'ionicons-min', get_template_directory_uri() . '/assets/css/ionicons.min.css' );
wp_enqueue_style( 'owl-carousel-min', get_template_directory_uri() . '/assets/css/owl.carousel.min.css' );
wp_enqueue_style( 'owl-theme-default-min', get_template_directory_uri() . '/assets/css/owl.theme.default.min.css' );
wp_enqueue_style( 'pe-icons-min', get_template_directory_uri() . '/assets/css/pe-icons.min.css' );
wp_enqueue_style( 'font-awesome-min', get_template_directory_uri() . '/assets/css/font-awesome.min.css' );
wp_enqueue_style( 'material-icons', get_template_directory_uri() . '/assets/css/material-icons.css' );
// Colors
wp_enqueue_style( 'color', get_template_directory_uri() . '/assets/css/colors/default.css' );
// JS Files
wp_enqueue_script( 'jquery-min', get_template_directory_uri() . '/assets/js/jquery.min.js', array(), '3.2.1', true );
wp_enqueue_script( 'owl-carousel-min', get_template_directory_uri() . '/assets/js/owl.carousel.min.js', array(), 'null', true );
wp_enqueue_script( 'slideNav', get_template_directory_uri() . '/assets/js/slideNav.js', array(), 'null', true );
wp_enqueue_script( 'main', get_template_directory_uri() . '/assets/js/main.js', array(), 'null', true );
wp_enqueue_script( 'wow-min', get_template_directory_uri() . '/assets/js/wow.min.js', array(), 'null', true );
wp_enqueue_script( 'bootbox-min', get_template_directory_uri() . '/assets/js/bootbox.min.js', array(), 'null', true );
wp_enqueue_script( 'bootstrap-min', get_template_directory_uri() . '/assets/js/bootstrap.min.js', array(), 'null', true );
wp_enqueue_script( 'isotope-min', get_template_directory_uri() . '/assets/js/isotope.min.js', array(), 'null', true );
}
add_action( 'wp_enqueue_scripts', 'load_scripts' );
You appear to have some sort of redirect on your site.
No matter what page you try to visit it always redirects to the main page.
I believe this is why the CSS and JS are not working because the web server just keeps redirecting all URL's to the main page.
I suggest looking at your .htaccess for any url rewrites and remove them. Once .htaccess is cleaned up, you can then verify if everything loads and displays properly.
Once working, you can research or post a question around fixing your .htaccess file, to redirect as you like.
<?php
// styles of the child theme
function uni_bauhaus_theme_child_styles() {
wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/css/font-awesome.min.css', array(), '4.5.0' );
wp_register_style( 'ball-clip-rotate-style', get_template_directory_uri() . '/css/ball-clip-rotate.css', '0.1.0' );
wp_enqueue_style( 'ball-clip-rotate-style');
wp_register_style( 'bxslider-style', get_template_directory_uri() . '/css/bxslider.css', '4.2.3' );
wp_enqueue_style( 'bxslider-style');
wp_register_style( 'fancybox-style', get_template_directory_uri() . '/css/fancybox.css', '2.1.5' );
wp_enqueue_style( 'fancybox-style');
wp_register_style( 'jscrollpane-style', get_template_directory_uri() . '/css/jscrollpane.css', '2.1.5' );
wp_enqueue_style( 'jscrollpane-style');
wp_register_style( 'unitheme-styles', get_template_directory_uri() . '/style.css', array('ball-clip-rotate-style', 'bxslider-style',
'fancybox-style', 'jscrollpane-style'), '1.3.1', 'all' );
wp_enqueue_style( 'unitheme-styles' );
wp_register_style( 'unitheme-adaptive', get_template_directory_uri() . '/css/adaptive.css', array('unitheme-styles'), false, 'screen' );
wp_enqueue_style( 'unitheme-adaptive' );
wp_register_style( 'unichild-styles', get_stylesheet_directory_uri() . '/style.css', array('unitheme-adaptive'), false, 'screen' );
wp_enqueue_style( 'unichild-styles' );
}
This is the enque function in the child theme. Updates to the css are only loaded if I make a change to the functions.php file and reupload it. Otherwise the site loads the old css. Turned off all cacheing.
If you enqueue your files with auto-versioning, you'll never have to worry about cache issues again:
$cssPath = get_stylesheet_directory() . '/style.css';
wp_enqueue_style(
'unichild-styles',
get_stylesheet_directory_uri() . '/style.css',
array(),
filemtime($cssPath)
);
This simply adds the time the css file was last saved as the version "number" ensuring zero cache problems locally or remotely :)
I have a script problem with a plugin and I have to deactivate a JS script from a certain page, the script is in functions.php
wp_enqueue_script( "helper-js", JS_PATH . 'helper.js', array( 'jquery' ), get_bloginfo('version') , true );
And I am trying to exclude it from single-room.php
You'd have to wrap the enqueue in a condition that checks the filename, something like
if ( basename( get_page_template() ) !== 'single-room.php') {
wp_enqueue_script( "helper-js", JS_PATH . 'helper.js', array( 'jquery' ), get_bloginfo('version') , true );
}