Using WordPress ‘admin_print_scripts’ PHP action

The admin_print_scripts WordPress PHP action fires when scripts are printed for all admin pages.

Usage

add_action('admin_print_scripts', 'your_custom_function');
function your_custom_function() {
    // your custom code here
}

Parameters

This action does not have any parameters.

More information

See WordPress Developer Resources: admin_print_scripts

Examples

Display a simple alert message on the admin dashboard

This example shows how to display an alert message when an admin page loads.

add_action('admin_print_scripts', 'display_alert_message');
function display_alert_message() {
    echo "<script>alert('Welcome to the Admin Dashboard!');</script>";
}

Enable jQuery UI tooltip on the admin dashboard

This example shows how to enable the jQuery UI tooltip for all admin pages.

add_action('admin_print_scripts', 'enable_jquery_ui_tooltip');
function enable_jquery_ui_tooltip() {
    echo "<script>jQuery(document).ready(function($) { $('body').tooltip({ items: '[data-tooltip]' }); });</script>";
}

Insert Google Analytics tracking code

This example shows how to insert Google Analytics tracking code in the admin area.

add_action('admin_print_scripts', 'insert_google_analytics_code');
function insert_google_analytics_code() {
    ?>
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
    ga('create', 'UA-XXXXX-Y', 'auto');
    ga('send', 'pageview');
    </script>
    <?php
}

Disable right-click context menu

This example shows how to disable the right-click context menu in the admin area.

add_action('admin_print_scripts', 'disable_right_click');
function disable_right_click() {
    echo "<script>document.addEventListener('contextmenu', function(e) { e.preventDefault(); });</script>";
}

Hide the admin bar

This example shows how to hide the admin bar using JavaScript.

add_action('admin_print_scripts', 'hide_admin_bar');
function hide_admin_bar() {
    echo "<script>document.addEventListener('DOMContentLoaded', function() { document.getElementById('wpadminbar').style.display = 'none'; });</script>";
}