35 votes

Using wp_add_inline_style without a stylesheet

You could simply use a "dummy" handle: wp_register_style( 'dummy-handle', false ); wp_enqueue_style( 'dummy-handle' ); wp_add_inline_style( 'dummy-handle', '* { color: red; }' );
Flix's user avatar
  • 451
31 votes
Accepted

How to add crossorigin and integrity to wp_register_style? (Font Awesome 5)

style_loader_tag style_loader_tag is an official WordPress API, see the documentation: https://developer.wordpress.org/reference/hooks/style_loader_tag/ apply_filters( 'style_loader_tag', $html, $...
Remzi Cavdar's user avatar
  • 1,176
21 votes
Accepted

How can I get a list of all enqueued scripts and styles?

do_action doesn't quite work like that. When you call do_action('crunchify_print_scripts_styles') WP looks at its list of registered actions and filters for any that are attached to a hook called ...
Andy Macaulay-Brook's user avatar
17 votes

How can I get a list of all enqueued scripts and styles?

You could use wp_print_scripts and wp_print_styles actions to timely and properly access to enqueued scripts and styles, as these actions are the last events before scripts and styles are included in ...
cybmeta's user avatar
  • 20.5k
17 votes

Load CSS/Javascript in frontend conditionally if block is used

Well, the styles and scripts you register in your php register_block_type call should only be loaded when the block is in use if i recall correctly. If you want to load additional scripts or styles ...
HU is Sebastian's user avatar
13 votes
Accepted

How to load scripts/styles specific for a page

It all depends on the scale of your customizations and how you will organize your stuff. But there is 2 main ways of doing it. functions.php and template files The way I like to do it, is register ...
bynicolas's user avatar
  • 2,279
10 votes
Accepted

The difference between calling wp_enqueue_scripts to load scripts and styles in custom theme

wp_enqueue_scripts Action Hook: WordPress provides various names (or place holders) that can be used to inject callback functions within WordPress core's execution lifecycle. These are called action ...
Fayaz's user avatar
  • 8,909
9 votes

Give priority to child theme stylesheet

Perhaps you can try adding a priority value to each add_action to make sure that one executes before the other. add_action( 'wp_enqueue_scripts', 'fruitful_load_parent_stylesheets', 10 ); add_action('...
goalsquid's user avatar
9 votes
Accepted

Loading a child-theme's style.css after the parent's

As @Andy Macaulay-Brook pointed out WordPress doesn't load child-theme's style.css . I guess parent theme might be en-queuing it. De-queue the child-theme style.css first and then enqueue it En-queue ...
bravokeyl's user avatar
  • 3,349
9 votes

Remove wp_add_inline_style

Remove styles added with wp_add_inline_style() If we want to keep the custom-style-css but only remove the custom-style-inline-css, then we can try e.g. add_action( 'wp_print_styles', function() { ...
birgire's user avatar
  • 67.1k
9 votes

Load CSS/Javascript in frontend conditionally if block is used

Another way is to enqueue them inside the block's render_callback() function. register_block_type( 'mkaz/test-block', array( 'editor_script' => 'mkaz-test-block-script', 'render_callback'...
Ian Dunn's user avatar
  • 4,837
9 votes
Accepted

How to dequeue / deregister any theme styles and scripts

The tricky thing is knowing whether or not a particular script or style was enqueued by the theme. Themes and plugins both use the same hooks and functions, so they're not explicitly labelled in any ...
Jacob Peattie's user avatar
8 votes
Accepted

Why is style.css not being enqueued?

The recommended way to do it is by enqueue-ing style.css in the functions.php of the theme. Add this to functions.php /** * Load CSS and JS the right way */ function myprefix_load_css_and_js() { ...
Arun Basil Lal's user avatar
8 votes

Enqueue custom font file with rel="preload"

You could try using the style_loader_tag filter. add_action('wp_enqueue_scripts', 'my_enqueue_scripts'); function my_enqueue_scripts() { wp_enqueue_style('my-style-handle', '/fonts/custom-...
DanLewis's user avatar
7 votes

wp enqueue style on specific page templates

If the page template is located in a subdirectory of the theme (since WP 3.4), prepend the folder name and a slash to the template filename, e.g.: is_page_template( 'templates/about.php' ); So, ...
Razon Komar Pal's user avatar
7 votes
Accepted

If is multiple page templates

I you want the script be enqueued on page-index.php and page-contact-us.php, then you must check that that page templates are used but you are checking if they are not used. Chagne this: if ( !...
cybmeta's user avatar
  • 20.5k
7 votes
Accepted

How to add a style to taxonomy edit page

You can do it like this: add_action ('admin_enqueue_scripts', 'wpse_style_tax') ; function wpse_style_tax () { // these 3 globals are set during execution of {edit-tags,term}.php global $...
Paul 'Sparrow Hawk' Biron's user avatar
7 votes
Accepted

Add last modified time as version to css and js

You could use add_query_arg() but then you'd have to parse the uri everytime, I'd rather create a wrapper function for wp_enqueue_script/style: function my_enqueuer($my_handle, $relpath, $type='...
inarilo's user avatar
  • 1,020
7 votes
Accepted

responsive.css in the Wordpress should be prioritized

If you want to make sure the second stylesheet loads after the first, you can use the third argument of wp_enqueue_style to set a dependency, eg: wp_enqueue_style( 'styles', THEMEROOT . '/css/style....
majick's user avatar
  • 5,081
6 votes

How do I Enqueue styles/scripts on Certain /wp-admin Pages?

As @mor7ifer mentioned above, you can use the native WordPress function get_current_screen(). If you loop through the output of this function, e.g.: $current_screen = get_current_screen(); foreach($...
recurse's user avatar
  • 173
6 votes

How to add extra attribute to stylesheet link?

We can use style_loader_tag filter to filter the link that is being output. Here is the filter: $tag = apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' ...
bravokeyl's user avatar
  • 3,349
6 votes

How to load scripts/styles specific for a page

"refactor", "optimise", "scalable", "elegant". Great concerns! You're on the right path. However splitting a CSS file into several is not the answer for these concerns. Here's why: Browsers cache CSS ...
zendka's user avatar
  • 266
6 votes
Accepted

Why is the Child Theme Stylesheet Not Loading?

There's no need for more code in your functions.php file to enqueue the parent themes css from your child theme. The problem is that by adding that code, you're enqueueing the parent theme's CSS a ...
Nathan Johnson's user avatar
6 votes
Accepted

wp_add_inline_style in plugin not working

Here's a solution based on this post, which allows the inline CSS to be rendered by a shortcode using a dependency without a path (basically a null file). // Optional base styles add_action( '...
Dave Romsey's user avatar
  • 17.7k
6 votes

Why my CSS file is not being registered in functions.php?

There are two useful action hooks for registering / enqueuing external scripts & styles in WordPress: 1. wp_enqueue_scripts: wp_enqueue_scripts is the proper action hook to use when enqueuing ...
Scott's user avatar
  • 1,315
6 votes
Accepted

Load CSS/Javascript in frontend conditionally if block is used

Thanks @kuchenundkakao for pointing me into the right direction. I'm still answering my own question as I want to give some more context for others that may stumble upon this. So, there are several ...
user127091's user avatar
6 votes
Accepted

is_page() function doesnt working

The init hook is too early — WordPress hasn't yet identified the queried object (post, category, etc.); hence the is_page() doesn't work there: add_action('init', 'register_script'); So for ...
Sally CJ's user avatar
  • 39.1k
6 votes

Enqueue script o style only if a template part is loaded

You can use the get_template_part_{$slug} hook, which fires before a template part is loaded. You can find the reference here, which I find more useful than the official reference. So, I tested it in ...
Laloptk's user avatar
  • 301
5 votes

wp enqueue style on specific page templates

I don't know if the solutions provided in other answers used to work, but (since there's no accepted answer!) it seems the correct answer is currently: function my_enqueue_stuff() { if ( ...
richplane's user avatar
  • 151
5 votes
Accepted

Give priority to child theme stylesheet

Your child theme's stylesheet will usually be loaded automatically. If it is not, you will need to enqueue it as well. Setting 'parent-style' as a dependency will ensure that the child theme ...
Charles's user avatar
  • 2,287

Only top scored, non community-wiki answers of a minimum length are eligible