Is it possible to use wp_localize_script to create global JS variables without a specific script handle?

Instead of using wp_localize_script in that case, you can hook your js variables at wp_head, that way it would be available to all js files like: function my_js_variables(){ ?> <script type=”text/javascript”> var ajaxurl=”<?php echo admin_url( “admin-ajax.php” ); ?>”; var ajaxnonce=”<?php echo wp_create_nonce( “itr_ajax_nonce” ); ?>”; </script><?php } add_action ( ‘wp_head’, ‘my_js_variables’ ) Also as suggested … Read more

How to add a javascript snippet to the footer that requires jQuery

The easiest way to do this is actually a combination of wp_enqueue_script and the footer actions that Saif and v0idless already referenced. I frequently use jQuery in my themes and plugins, but I put all of the other scripts in the footer as well. The best way to actually queue things up would be this: … Read more

How to add defer=”defer” tag in plugin javascripts?

As of WordPress 4.1 there is a filter: script_loader_tag. You can use it to find the correct script: add_filter( ‘script_loader_tag’, function ( $tag, $handle ) { if ( ‘contact-form-7’ !== $handle ) return $tag; return str_replace( ‘ src’, ‘ defer=”defer” src’, $tag ); }, 10, 2 ); Old answer There is no dedicated filter available … Read more

wp enqueue style on specific page templates

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’) conditional check instead.

Check if a script/style was enqueued/registered

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

Enqueue core jQuery in the footer?

To do that you will first have to deregister your jQuery script and then register again. If you use jQuery comes with WordPress then following is the function your are looking for. function starter_scripts() { wp_deregister_script( ‘jquery’ ); wp_register_script( ‘jquery’, includes_url( ‘/js/jquery/jquery.js’ ), false, NULL, true ); wp_enqueue_script( ‘jquery’ ); wp_enqueue_style( ‘starter-style’, get_stylesheet_uri() ); wp_enqueue_script( … Read more

ajaxurl not defined on front end

In backend there is global ajaxurl variable defined by WordPress itself. This variable is not created by WP in frontend. It means that if you want to use AJAX calls in frontend, then you have to define such variable by yourself. Good way to do this is to use wp_localize_script. Let’s assume your AJAX calls … Read more

How do I dequeue a parent theme’s CSS file?

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’ ); wp_deregister_script( ‘site’ ); // Now register your styles … Read more

wp enqueue inline script due to dependancies

Well, you have wp_localize_script(), but that’s only for passing data. Otherwise, you can do this: function print_my_inline_script() { if ( wp_script_is( ‘some-script-handle’, ‘done’ ) ) { ?> <script type=”text/javascript”> // js code goes here </script> <?php } } add_action( ‘wp_footer’, ‘print_my_inline_script’ ); The idea is that you shouldn’t rely on your inline script being printed … Read more

Conditionally Loading JavaScript/CSS for Shortcodes

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 … Read more