Hot answers tagged

55

Why registering and queuing properly matters it should be in time - earlier than script/style is up for being output to page, otherwise it is too late; it should be conditional - otherwise you are loading stuff where you don't need it and cause performance and functionality issues, for this you need WP environment loaded to late stage. The three stages ...


41

There is a function called wp_script_is( $handle, $list ). $list can be one of: 'registered' -- was registered through wp_register_script() 'queue' -- was enqueued through wp_enqueue_script() 'done' -- has been printed 'to_do' -- will be printed Ditto all that for wp_style_is().


32

The problem with @tollmanz answer is that since you're hooking off of the -print-styles and -print-scripts hooks, you must generate the HTML to load your scripts manually. This is not optimal, since you don't get the nice dependency and versioning that comes with wp_enqueue_script() and wp_enqueue_style(). It also doesn't let you put things in the footer if ...


25

I want to use @import instead so I can override styles more easily Simply. Don't. Do. That. You simply jump into the same hook and then deregister/dequeue the styles/scripts and throw in your custom ones. function PREFIX_remove_scripts() { wp_dequeue_style( 'screen' ); wp_deregister_style( 'screen' ); wp_dequeue_script( 'site' ); ...


23

For every enqueue action, there is a corresponding print_styles action: wp_enqueue_scripts → wp_print_styles admin_enqueue_scripts → admin_print_styles customize_controls_enqueue_scripts → customize_controls_print_styles Not for the login page. There is no login_print_styles action or function, so your stylesheets are thrown out on do_action( ...


22

WordPress knows what it is doing here. Honest. When rendering an ampersand in HTML, you should always use & or &. The browser then converts it to & before actually firing the HTTP request. See for yourself by inspecting the network calls in a web inspector tool. You're not actually losing your non-latin subsets. Notice the ...


17

add_menu_page and add_submenu_page both return the page's "hook suffix", which can be used to identify the page with certain hooks. As such, you can use that suffix in combination with the variable hooks admin_print_styles-{$hook_suffix} and admin_print_scripts-{$hook_suffix} to specifically target these pages. function my_menu() { $menu = add_menu_page( ...


16

I hope you know what you are doing. You can wp_print_styles and wp_print_scripts action hooks and then get the global $wp_styles and $wp_scripts object variables in their respective hooks. The "registered" attribute lists registered scripts and the "queue" attribute lists queue scripts on both of the above objects. An example code to empty the scripts and ...


16

It's important to enqueue the stylesheet because it will allow child themes the flexibility of dequeueing it as well as allowing it to be listed for dependencies and a multitude of other things. It just generally allows greater flexibility, both for you and for any other developers who interact with your code. It's also important to note that using the ...


15

As an addition to the other answer by @m0r7if3r: You can use current_theme_supports() to only load the parent themes stylesheet if there's theme support. function add_supported_stylesheets() { if ( current_theme_supports( 'parent-stylesheet' ) ) wp_enqueue_style( 'main', get_stylesheet_directory_uri().'/style.css', array(), filemtime( ...


15

You can use the is_page( 'landing-page-template-one' ) conditional around your page specific styles / scripts as part of your over-all enqueue statements. function my_enqueue_stuff() { if ( is_page( 'landing-page-template-one' ) ) { /** Call landing-page-template-one enqueue */ } else { /** Call regular enqueue */ } } add_action( ...


14

Theme stylesheets aren't usually enqueued, they're normally loaded using.. <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" /> So naturally you don't see them(it) in the styles array.. You can of course(if you prefer) use an enqueue instead.


14

TL;DR; Enqueue Using external stylesheet PRO: All your styles are in one spot. PRO: Reduces web page coding. PRO: Easier to maintain the plugin. PRO: Can use hooks to alter location of the file. PRO: Can use hooks to unqueue the file. PRO: Can use minify styles automatically. CON: Might add extra HTTP request (can be overcome). Using inline styles ...


13

If you use get_current_screen(), you can detect what the page you're on is. There is an example in the codex article that I linked which shows how to use get_current_screen() with add_options_page(), this method will work for any admin page.


12

I found a solution that works for me! I dropped this code in functions.php : add_action('admin_head', 'my_column_width'); function my_column_width() { echo '<style type="text/css">'; echo '.column-mycolumn { text-align: center; width:60px !important; overflow:hidden }'; echo '</style>'; }


12

As far as I know, there is not style for datepicker. You have to register your own. The code then will be: function rr_scripts() { wp_enqueue_script( 'jquery' ); wp_enqueue_script( 'jquery-ui-datepicker', array( 'jquery' ) ); wp_register_style( 'bootstrap_css', get_template_directory_uri() . '/assets/css/bootstrap.min.css' ); wp_enqueue_style( ...


11

The right hooks // Use both for scripts & styles *) wp_enqueue_scripts // (for the frontend) login_enqueue_scripts // (for the login screen) admin_enqueue_scripts // (for the admin dashboard) *) Read this article @wpdevel. Further reading in the Codex about the three hooks admin_menu network_admin_menu user_admin_menu On the ...


11

Found the answer here: Script dequeuing calls should be added to the wp_print_scripts action hook(..). This is because scripts are typically enqueued on the wp_enqueue_script hook, which happens early in the wp_head process. The wp_print_scripts hook happens right before scripts are printed, and thus is latest in the process. (Otto) Following ...


11

Enqueue the style.css too, and set normalize as dependency: function load_css_files() { wp_register_style( 'normalize', get_template_directory_uri() . '/css/normalize.css'); wp_register_style( 'theme_name', get_stylesheet_uri(), array( 'normalize' )); wp_enqueue_style( 'theme_name' ); } WordPress will load the dependencies now first ...


11

First of all let's say that regarding these functions what is valid for styles is exactly valid for scripts, but there are some exceptions to the contrary explained in the answer. Main difference between wp_enqueue_* and respective wp_register_* functions, is that the first adds scripts/styles to the queue, the second prepares scripts/styles to be added. ...


10

Check $GLOBALS['wp_scripts']->registered for scripts. Example function is_enqueued_script( $script ) { return isset( $GLOBALS['wp_scripts']->registered[ $script ] ); } print (int) is_enqueued_script( 'l10n' ); $GLOBALS['wp_styles']->registered works the same way.


9

Based on my own experience, I've used a combination of method 1 & 2 - the architecture and footer scripts of 1, and the 'look-ahead' technique of 2. For the look-ahead though, I use regex in place of stripos; personal preference, faster, and can check for 'malformed' shortcode; preg_match( '#\[ *shortcode([^\]])*\]#i', $content ); If you're concerned ...


9

You should be able to just use http://fonts.googleapis.com/css?family=Rosario:400,700,400italic which is the "combined" URL given by Google when selecting multiple weights/styles of a font. You can then just register the font once. You don't need to over-complicate the $handle either, something like google-fonts-rosario should do just fine as long as it is ...


9

This is what you could do: 1 - Put de CSS in a separate file and save it in your theme directory. 2 - put the following code in your functions php: function wpse_89494_enqueue_scripts() { if ( has_nav_menu( 'secondary' ) ) { wp_enqueue_style( 'wpse_89494_style_1', get_template_directory_uri() . '/your-style_1.css' ); } if ( ...


8

The problem is that the wp_enqueue_style() call is inside of the category_collapse() member function of the CategoryCollapse() class, and the CategoryCollapse() class is being instantiated by a callback hooked into the plugins_loaded action hook. That means that the wp_enqueue_style() function is attempting to execute at the plugins_loaded hook, which fires ...


8

If you plan to do a lot of WP development you should bookmark this page: http://codex.wordpress.org/Conditional_Tags The other answer works but the conditional relies upon your page slug (myurl.com/this-is-the-slug) never changing. A more reliable method (IMO), and one that fits this case, would be to use the is_page_template('example-template.php') ...


8

Script dequeuing calls should be added to the wp_print_scripts action hook, like so: add_action('wp_print_scripts','example_dequeue_myscript'); function example_dequeue_myscript() { wp_dequeue_script( 'myscript' ); } This is because scripts are typically enqueued on the wp_enqueue_script hook, which happens early in the wp_head process. The ...


7

Take a look here at the CODEX for an example on how to do this very thing. Example: Load CSS File on All Admin Pages function load_custom_wp_admin_style(){ wp_register_style( 'custom_wp_admin_css', get_bloginfo('stylesheet_directory') . '/admin-style.css', false, '1.0.0' ); wp_enqueue_style( 'custom_wp_admin_css' ); } ...


7

Try this: Using query conditionals Putting style enqueueing directly in the template is not advisable. It is possible (if you understand the order that relevant actions fire, and ensure that your call to wp_add_inline_style() happens at the right point in the execution order). It is far easier to keep all the code together, and use an appropriate query ...


6

wp_print_scripts and wp_print_styles hooks are fired way before your widget function so that is way it's not working. A solution to that would be to include the scripts in the footer using wp_print_footer_scripts hook, take a look at Jan's answer to a similar question Or a much nicer solution take a look at Sorich's answer to another similar question



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