How do I add a javascript file to all admin pages via a plugin?

The Plugin Developer Handbook recommends using plugins_url() to create the URL to the JS file. Try using that function inside your script loader, and using a different handle for the script depending on the context (like prefixing the admin script name with “admin-“) to avoid conflicting script registration. Alternatively, you can register the script just … Read more

How do you output enqueued scripts to an admin page?

To display registered scripts information in posts/pages, we’ll use a shortcode. For admin registered scripts, we’ll use admin_footer action hook. Put this code into your plugin: if(!is_admin) { function fpwScripts($atts) { global $wp_scripts; $out=”<h2>Registered Scripts</h2>”; foreach($wp_scripts->registered as $key => $value) { $out .= “<h3>{$value->handle}</h3>”; $out .= “source = {$value->src}<br>”; $out .= “dependencies = array(<br>”; foreach($value->deps … Read more

Help with enqueing scripts in footer after init action

If you enqueue the script to the footer while the page is being rendered (after the “wp_head” hook), you will have to manually add wp_print_scripts (or wp_print_footer_scripts) to the wp_footer action, like this: wp_enqueue_script( ‘jquery-gallery’, null, array( ‘jquery’ ), null, true ); add_action( ‘wp_footer’, create_function( ”, ‘wp_print_scripts( “jquery-gallery” );’ ) );

Performance-wise, is it better to enqueue a (small) script on every page or test to see if it’s needed?

What happens on the user’s side is off topic as @bungeshea said already. Once the user has loaded the file it will stay in the browser cache for a while, so you don’t have to worry about double loaded files anyway. But let’s look at the server side and how WordPress handles this. If you … Read more

Register script/style: Is it possible to customize the version query string via plugin?

Old answer (based on misconception that you wanted a cache buster): You can use add_query_arg() which adds/replaces query arguments. <?php /** * Plugin Name: wpse_84670 * Version: 0.0.1 * Description: replace script/style version with time as a cache buster */ /** * replace script or stylesheet version with current time * @param string $url the … Read more

How to detect if a function has been fired on any page so scripts/styles can be loaded conditionally

For scripts this is no-brainer – just enqueue inside my_gallery() function and they will be output in footer (since header already passed). However stylesheets are a challenge, they are only supposed to work in head section and enqueueing for footer is not supported. Essentially you need to know if function was called before it was … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)