Tag Info

Hot answers tagged

49

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 ...


35

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().


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 ...


21

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 ...


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 ...


16

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' ); ...


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

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( ...


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 ...


14

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( ...


12

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 ...


10

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.


10

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 ...


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

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 Admin page ...


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

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.


8

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 ...


8

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.


8

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( ...


7

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 ...


6

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' ); } ...


6

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 ( ...


6

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 ...


5

Why are my styles being applied to the admin area? Assuming that is all of the code, it is because you are enqueueing the styles globally. You need to hook that to wp_enqueue_scripts, which will only load on the front end. wp_register_style( 'main-styles', get_stylesheet_directory_uri() . '/css/styles.css', false, '1.0.0', 'all'); function ...


5

I'm late answering this question but since Ian started this thread on the wp-hackers list today it made me think it worth answering especially considering I've been planning to add such a feature to some plugins I've been working on. An approach to consider is to check on the first page load to see if the shortcode is actually used and then save the ...


5

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( ...


5

Try this (will also handle HTTP vs HTTPS): function wpse_google_webfonts() { $protocol = is_ssl() ? 'https' : 'http'; $query_args = array( 'family' => 'Ubuntu+Condensed|Open+Sans:400italic,700italic,400,700', 'subset' => 'latin,latin-ext', ); wp_enqueue_style('google-webfonts', add_query_arg($query_args, ...


5

You are using plugins_url - therefore pointing to completly different directory than your current theme. If you are just trying to enqueue a theme style do it like this: function load_theme_styles() { wp_enqueue_style('main-css', get_template_directory_uri() . '/style.css', array(), '1.0', 'all'); } add_action('wp_enqueue_scripts', ...


5

You should always use wp_enqueue_style() and wp_enqueue_script() where possible, unless it it a special case such as the HTML5 Shiv script which requires IE conditional comments. If you use the proper hooks to enqueue your style, it should only be loaded on the front-end. The correct hook to use in your instance would be wp_enqueue_scripts, which works for ...



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