Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use admin_print_scripts action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
June 19, 2023
5 minutes read

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.

Access Premium WordPress Plugins

If you need any customization or are facing issues with this hook, feel free to Contact Us. We’re here to help!

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

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.