In order to link my stylesheets to my WordPress theme, I have the following inside my customtheme/style.css:
#import url('bootstrap/bootstrap.min.css');
#import url('includes/styles1.css');
#import url('includes/styles2.css');
Suppose I wanted to load styles1.css on only one page (let's say the home-page) and load styles2.css on all the other pages. Is there anyway to specify this?
wp_register_style and wp_enqueue_style
How it works:
wp_register_style allows you to register your own stylesheets and give them their own handles. What this enables you to do is define all of your styles and load them on an as-needed basis. In many cases, you will often see stylesheets registered early on in the theme's lifecycle, and then enqueued later based on some logic checks.
As an example:
Let's say you have some custom shortcode, but don't want to load any of its stylesheets unless the shortcode itself is actually used:
functions.php
add_action('wp_enqueue_scripts', 'custom_enqueue_scripts');
function custom_enqueue_scripts()
{
//Register the style
wp_register_style('my-shortcode-styles', get_template_directory_uri() . '/css/shortcode-styles.css');
}
add_action('init', 'custom_init');
function custom_init()
{
//Example shortcode
add_shortcode('my_shortcode', 'custom_shortcode');
}
function custom_shortcode($atts, $content = null)
{
//If registered style isn't loaded....
if (!wp_style_is('my-shortcode-styles')) {
//Enqueue it!
wp_enqueue_style('my-shortcode-styles');
}
return 'My Shortcode!';
}
In most cases though, wp_enqueue_style will be sufficient. Using this, you can register and enqueue your stylesheets all at once:
add_action('wp_enqueue_scripts', 'custom_enqueue_scripts');
function custom_enqueue_scripts()
{
//Register and enqueue the style
wp_enqueue_style('my-shortcode-styles', get_template_directory_uri() . '/css/shortcode-styles.css');
}
In your case, you can perform some quick logic checks to determine what page the user is visiting before loading the appropriate stylesheet:
add_action('wp_enqueue_scripts', 'custom_enqueue_scripts');
function custom_enqueue_scripts()
{
if(is_home()){ //is_front_page() if you're using a Page for the home page
//Load only on the home page
wp_enqueue_style('home-styles', get_template_directory_uri() . '/css/styles1.css');
}
//Load everywhere else
wp_enqueue_style('my-theme', get_template_directory_uri() . '/css/styles2.css');
}
Quick note: In order for stylesheet enqueues to work, your theme MUST use wp_head and wp_footer. If your active theme is missing these in its template files, then stylesheet enqueues will not work.
See also:
wp_register_script
wp_enqueue_script
Why do you want to load different stylesheets on different pages? Having minimal sheets would result to better score in https://tools.pingdom.com and/or https://developers.google.com/speed/pagespeed/insights/ and faster loading time. Just target specific class or id in one stylesheet for your different pages and you are golden.
Related
I used these two plugins named "Everest-tab-lite" and "easy-testimonials" on only one of my pages with id="15280", but their CSS and JS scripts load on all of the pages. For restricting the scripts I tried two ways but, did not work. I don't know what I did wrong. Is there any way to solve this issue?
The first way, I added bellow code at the end of functions.php file.
function conditionally_load_plugin_js_css(){
if(! is_page( array(15280) ) ){ # Load CSS and JS only on Pages with ID 4 and 12
wp_dequeue_script('easy-testimonials'); # Restrict scripts.
wp_dequeue_style('easy-testimonials'); # Restrict css.
wp_dequeue_script('everest-tab-lite'); # Restrict scripts.
wp_dequeue_style('everest-tab-lite'); # Restrict css.
}
}
add_action( 'wp_enqueue_scripts', 'conditionally_load_plugin_js_css' );
The second way, I added the bellow code at the end of functions.php file.
function remove_wpetl_extras() {
remove_action('wp_print_scripts', 'wpetl_enqueue_scripts');
remove_action('wp_print_styles', 'wpetl_enqueue_styles');
}
if( ! is_page('15280') ) {
add_action('wp_head', 'remove_wpetl_extras');
}
I guess you just need to check for order priority
priority
(int) (Optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. Default value: 10 Docs
add_action( 'wp_enqueue_scripts', 'conditionally_load_plugin_js_css', 100);
Change the priority to 100 or higher 999 because most plugins uses high values like 99 or even more and you need to override that.
Also please note that if you want to completely remove a style or script, you need to dequeue your scripts then deregister it.
Edit:
I see the problem. I've tested the code and it works but you must write the Exact Name of the style/script.
In your case, Both plugins adds some stylesheets and scripts, Easy Testimonials adds 6 stylesheets and 2 scripts:
easy_testimonial_style
single-testimonial-block
random-testimonial-block
testimonials-list-block
testimonials-cycle-block
testimonials-grid-block
easy-testimonials-reveal
gp_cycle2 (jquery.cycle2)
And Everest Tab Lite adds 3 more stylesheets and 1 script:
et-frontend-style
et_fontawesome_style
et-animate-style
et-frontend-script
Please note that WordPress will automatically add -css at the end of
the stylesheet name. i.e et-frontend-style will show as
et-frontend-style-css
So your function would be:
function conditionally_load_plugin_js_css() {
// Load CSS and JS only on Pages with 15280
if(! is_page( array(15280) ) ) {
// Easy testimonial
wp_dequeue_style('easy_testimonial_style');
wp_dequeue_style('single-testimonial-block');
wp_dequeue_style('random-testimonial-block');
wp_dequeue_style('testimonials-list-block');
wp_dequeue_style('testimonials-cycle-block');
wp_dequeue_style('testimonials-grid-block');
// scripts
wp_deregister_script('gp_cycle2');
wp_deregister_script('easy-testimonials-reveal');
// Everest Tab Lite
wp_dequeue_style('et-frontend-style');
// Keep these if you like fontawesome and animate.css
wp_dequeue_style('et_fontawesome_style');
wp_dequeue_style('et-animate-style');
// script
wp_deregister_script('et-frontend-script');
}
}
// Everest Tab Lite has priority more than 9999 so i changed the priority to 99999
add_action( 'wp_enqueue_scripts', 'conditionally_load_plugin_js_css', 99999);
I have a plugin, it has many css theme files. Of course I do not want to load all of them, only one. It depends on config. For post I use has_shortcode function, but how todo the same thing with template that use do_shortcode function.
Note:
I found a good solution, I use
$this->loader->add_action( 'init', $plugin_public, 'register_scripts');
$this->loader->add_action( 'wp_footer', $plugin_public, 'print_scripts');
Inside shortcode handle I set a global var to true
global $imagelink_plugin_shortcode_used;
$imagelink_plugin_shortcode_used = true;
The function print_scripts add my scripts if my global var is true
public function print_scripts() {
global $imagelink_plugin_shortcode_used;
if ( ! $imagelink_plugin_shortcode_used )
return;
wp_print_scripts($this->plugin_name . '-imagelinks');
}
Thanks for answers.
Instead of using has_shortcode function, what you can do is register and enqueue those files when that shortcode is rendered.
First, register your css files with wp_register_script. Make sure to hook this into wp_enqueue_scripts
Now, inside your shortcode function, enqueue the files.
wp_enqueue_style('theme-css')
Using this way you can use the shortcode anywhere you want and the script is loaded only when shortcode is present on the page whether it is on content or template.
I have added this line on my plugin
wp_enqueue_style( 'dazzling-bootstrap', get_template_directory_uri() . '/inc/css/bootstrap.min.css' );
and it seems that the whole admin backend was affected of that bootstrap. Any ideas on how to be only on that plugin?
That's because you're not specifying anywhere that the file should be included only for your plugin page, and not for the whole admin backend. Try to add a conditional check and then enqueue the stylesheet.
global $post;
if ( 'enter_plugin_page_slug_here' == $post->name ) {
// enqueue stylesheet here
}
I am building a full design into WordPress for the first time and I am trying to load in stylesheets and script files but all I seem to be getting is the text output of the location.
What I have is below..
wp_enqueue_style('reset', bloginfo('template_url') . '/reset.css');
wp_enqueue_style('style', bloginfo('stylesheet_url'), array('reset'));
wp_enqueue_style('rhino', bloginfo('template_url') . '/rhinoslider-1.05.css', array('reset','style'));
Do I need to put this inside the link tags or something? I thought it would do it all itself; as what's the point loading it that way if it doesn't do it itself? I know it makes sure the same file isn't included twice or something, but if you have to include the link tags yourself and then WP decides not to include the file then you are left with blank link tags!?
Lastly, should I set these up beforehand so I can just call them via their handles? If so, where? functions.php?
Edit: I also tried putting the below in my themes functions.php file but got the same results.
add_action( 'after_setup_theme', 'mmw_new_theme_setup' );
function mmw_new_theme_setup() {
/* Add theme support for post formats. */
add_theme_support( 'post-formats' );
/* Add theme support for post thumbnails. */
add_theme_support( 'post-thumbnails' );
/* Add theme support for automatic feed links. */
add_theme_support( 'automatic-feed-links' );
/* Add theme support for menus. */
add_theme_support( 'menus' );
/* Load style files on the 'wp_enqueue_scripts' action hook. */
add_action( 'wp_enqueue_scripts', 'mmw_new_load_styles' );
}
function mmw_new_load_styles() {
$foo = bloginfo('template_url') . '/reset.css';
$bar = bloginfo('template_url') . '/rhinoslider-1.05.css';
wp_enqueue_style('reset', $foo);
wp_enqueue_style('style', bloginfo('stylesheet_url'), array('reset'));
wp_enqueue_style('rhino', $bar, array('reset','style'));
}
When storing values in a variable via PHP use:
get_bloginfo()
So your new function would now look like:
function mmw_new_load_styles() {
$foo = get_bloginfo('template_url') . '/reset.css';
$bar = get_bloginfo('template_url') . '/rhinoslider-1.05.css';
wp_enqueue_style('reset', $foo);
wp_enqueue_style('style', bloginfo('stylesheet_url'), array('reset'));
wp_enqueue_style('rhino', $bar, array('reset','style'));
}
And be more semantic! It makes code for beginners easier to look at. ($foo could be $resetCssUrl)
I was having similar issues.
The register / enqueue scripts are so that you can globally assign your functions to load in the correct order. You can call them from the page that your working in but it is considered better practice do it this way.
My template has a functions.php but its nealry empty! It sepreates the scripts into 7 subchapters, theme-options, theme-functions, themes-js, etc. Here is my themes.js.php file but this could quite easily placed in your file inside your wp-content/themes/functions.php My themes-js.php file
I'm developing a WordPress widget following Dave Clements tutorial. It works well. Now I want to add some styles to it. I want the styles to be in an extra css file which will be loaded during runtime. I'm calling this function
function myprefix_add_my_stylesheet() {
wp_register_style( 'myprefix-style', plugins_url('mystyle.css', __FILE__) );
wp_enqueue_style( 'myprefix-style' );
}
right before "// Widget output //" using this statement.
add_action( 'wp_enqueue_scripts', 'myprefix_add_my_stylesheet' );
But nothing seems to happen. What am I doing wrong?
Your wp_enqueue_scripts action do not work because you call it too late, in widget() function (widget output). It's too late because Wordpress already print/send head of page.
For example, you can add this action on widget construct function.