This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
So, you’re diving into the world of WordPress hooks, huh? It’s a pretty cool journey, and today we’re talking about the admin_print_scripts action. This hook is your go-to when you need scripts to be printed for all admin pages. To use it, you’ll first register it using add_action. You can drop this code into your theme’s functions.php or even better, in a custom WordPress plugin. At WePlugins, we always suggest creating a custom plugin to keep your theme updates smooth and worry-free.
Example 1: Adding Inline JavaScript
Want to add some inline JavaScript to your admin pages? Here’s how you can achieve that using the admin_print_scripts hook.
function weplugins_admin_inline_js(){ echo "<script type='text/javascript'>n"; echo 'var pluginUrl = ' . wp_json_encode( WP_PLUGIN_URL . '/my_plugin/' ) . ';'; echo "n</script>"; } add_action( 'admin_print_scripts', 'weplugins_admin_inline_js' );
Example 2: Executing Custom Function
Let’s see how you can execute a custom function when this action occurs in your WordPress site.
function weplugins_execute_on_admin_print_scripts_event(){ // Your custom code here } // Add the action add_action( "admin_print_scripts", "weplugins_execute_on_admin_print_scripts_event");
Example 3: Removing a Hook
Need to remove a previously registered hook? No worries, here’s how you do it.
remove_action( "admin_print_scripts", "weplugins_execute_on_admin_print_scripts_event");
Remember to provide the same callback function name, priority, and the number of arguments when removing the hook callback.
If you need any customization or are facing issues with this hook, feel free to Contact Us. We’re here to help!
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.