Add Admin JavaScript

plugin banner

Version: 1.1.1

Interface for easily defining additional JavaScript (inline and/or by URL) to be added to all administration pages.

Author:Scott Reilly (profile at wordpress.org)
WordPress version required:4.9
WordPress version tested:5.5.1
Plugin version:1.9.1
Added to WordPress repository:05-12-2011
Last updated:07-12-2019
Rating, %:96
Rated by:5
Plugin URI:http://coffee2code.com/wp-plugins/add-admin-j...
Total downloads:26 944
Active installs:3 000+
plugin download
Click to start download

Ever want to introduce custom dynamic functionality to your WordPress admin pages and otherwise harness the power of JavaScript? Any modification you may want to do with JavaScript can be facilitated via this plugin.

Using this plugin you’ll easily be able to define additional JavaScript (inline and/or by URL) to be added to all administration pages. You can define JavaScript to appear inline in the admin head, admin footer (recommended), or in the admin footer within a jQuery jQuery(document).ready(function($)) {} section, or reference JavaScript files to be linked in the page header. The referenced JavaScript files will appear in the admin head first, listed in the order defined in the plugin’s settings. Then any inline admin head JavaScript is added to the admin head. All values can be filtered for advanced customization (see Filters section).

Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage

Hooks

The plugin exposes four filters for hooking. Typically, code making use of filters should ideally be put into a mu-plugin or site-specific plugin (which is beyond the scope of this readme to explain). Bear in mind that most of the features controlled by these filters are configurable via the plugin’s settings page. These filters are likely only of interest to advanced users able to code.

c2c_add_admin_js_files (filter)

The ‘c2c_add_admin_js_files’ filter allows programmatic modification of the list of JavaScript files to enqueue in the admin.

Arguments:

  • $files (array): Array of JavaScript files.

Example:

/**
 * Adds a JavaScript file to be enqueued in the WP admin.
 *
 * @param array $files Array of files.
 * @return array
 */
function my_admin_js_files( $files ) {
    $files[] = 'http://ajax.googleapis.com/ajax/libs/yui/2.8.1/build/yuiloader/yuiloader-min.js';
    return $files;
}
add_filter( 'c2c_add_admin_js_files', 'my_admin_js_files' );

c2c_add_admin_js_head (filter)

The ‘c2c_add_admin_js_head’ filter allows customization of the JavaScript that should be added directly to the admin page head.

Arguments:

  • $js (string): JavaScript code (without <script> tags).

Example:

/**
 * Adds JavaScript code to be added to the admin page head.
 *
 * @param string $js JavaScript code.
 * @return string
 */
function my_add_head_js( $js ) {
    $js .= "alert('Hello');";
    return $js;
}
add_filter( 'c2c_add_admin_js_head', 'my_add_head_js' );

c2c_add_admin_js_footer (filter)

The ‘c2c_add_admin_js_footer’ filter allows customization of the JavaScript that should be added directly to the admin footer.

Arguments:

  • $js (string): JavaScript code (without <script> tags).

Example:

/**
 * Adds JavaScript code to be added to the admin footer.
 *
 * @param string $js JavaScript code.
 * @return string
 */
function my_add_footer_js( $js ) {
    $js .= "alert('Hello');";
    return $js;
}
add_filter( 'c2c_add_admin_js_footer', 'my_add_footer_js' );

c2c_add_admin_js_jq (filter)

The ‘c2c_add_admin_js_jq’ filter allows customization of the JavaScript that should be added directly to the admin footer within a jQuery document ready function.

Arguments:

  • $jq_js (string): JavaScript code (without <script> tags or jQuery document ready function).

Example:

/**
 * Adds jQuery code to be added to the admin footer.
 *
 * @param string $jq_js jQuery code.
 * @return string
 */
function my_add_jq( $js_jq ) {
    $js_jq .= "$('.hide_me').hide();";
    return $js_jq;
}
add_filter( 'c2c_add_admin_js_jq', 'my_add_jq' );

Highlights:

  • This minor release adds support for themes that don’t explicitly support HTML5, tweaks plugin initialization, modernizes and fixes unit tests, and notes compatibility through WP 5.3+.

Details:

  • New: Add non-HTML5 compliance by specifying type attribute when the theme doesn’t explicitly support ‘html5’
  • Change: Check that code is running in the admin just before registering hooks and not before defining class
  • Unit tests:
    • New: Add unit tests for recovery mode’s admin notice
    • New: Add assertion that recovery mode is not enabled if query param is present but false
    • Fix: Don’t pass argument to plugin object’s add_js_to_head() and add_js_to_food(), which don’t support arguments
    • Fix: Prevent WP from attempting to print the emoji detection script (which isn’t built in the develop.svn repo)
    • Change: Update unit test install script and bootstrap to use latest WP unit test repo
    • Change: Ensure admin mode is enabled before running certain tests
    • Change: Rename test_can_show_js_with_false_query_param() to test_can_show_js_with_true_query_param() to better reflect its intent
    • Change: Use dirname() instead of relative path syntax
    • Change: Remove unnecessary action performed during teardown
  • Change: Note compatibility through WP 5.3+
  • Change: Tweak installation instruction
  • Change: Tweak description of “Hooks” section in readme.txt
  • Change: Update copyright date (2020)

1.7 (2019-04-09)

Highlights:

  • This release adds a recovery mode to disable output of JavaScript via the plugin (and an admin notice when it is active), replaces code input fields with code editor (with syntax highlight, syntax checking, code completion, and more), improves documentation, updates the plugin framework, notes compatibility through WP 5.1+, drops compatibility with versions of WP older than 4.7, and more documentation and code improvements.

Details:

  • New: Add syntax highlighting to JavaScript input fields
    • Adds code highlighting, syntax checking, and other features
  • New: Add recovery mode to be able to disable output of JavaScript via the plugin
    • Add support for c2c-no-js query parameter for enabling recovery mode
    • Add support for C2C_ADD_ADMIN_JAVASCRIPT_DISABLED constant for enabling recovery mode
    • Display admin notice when recovery mode is active
    • Add can_show_js(), remove_query_param_from_redirects(), recovery_mode_notice()
  • Change: Initialize plugin on plugins_loaded action instead of on load
  • Change: Update plugin framework to 049
    • 049:
    • Correct last arg in call to add_settings_field() to be an array
    • Wrap help text for settings in label instead of p
    • Only use label for help text for checkboxes, otherwise use p
    • Ensure a textarea displays as a block to prevent orphaning of subsequent help text
    • Note compatibility through WP 5.1+
    • Update copyright date (2019)
    • 048:
    • When resetting options, delete the option rather than setting it with default values
    • Prevent double “Settings reset” admin notice upon settings reset
    • 047:
    • Don’t save default setting values to database on install
    • Change “Cheatin’, huh?” error messages to “Something went wrong.”, consistent with WP core
    • Note compatibility through WP 4.9+
    • Drop compatibility with version of WP older than 4.7
  • Change: Remove unnecessary type='text/javascript' attribute from <script> tags
  • New: Add README.md file
  • New: Add CHANGELOG.md file and move all but most recent changelog entries into it
  • New: Add FAQ entry describing ways to fix having potentially crippled the admin
  • New: Add inline documentation for hooks
  • New: Add GitHub link to readme
  • Unit tests:
    • Change: Improve tests for settings handling
    • Change: Update set_option() to accept an array of setting values to use
    • New: Add unit tests for add_js_to_head(), add_js_to_foot()
    • New: Add unit test for defaults for settings
    • Remove: Delete setUp() and invoke setup_options() within each test as needed
    • Remove: Delete private object variable for storing setting name
  • Change: Store setting name in constant
  • Change: Improve documentation for hooks within readme.txt
  • Change: Use alternative example remote JS library to the defunct Yahoo UI library
  • Change: Note compatibility through WP 5.1+
  • Change: Drop compatibility with version of WP older than 4.7
  • Change: Rename readme.txt section from ‘Filters’ to ‘Hooks’
  • Change: Modify formatting of hook name in readme to prevent being uppercased when shown in the Plugin Directory
  • Change: Update installation instruction to prefer built-in installer over .zip file
  • Change: Update copyright date (2019)
  • Change: Update License URI to be HTTPS

1.6 (2017-11-03)

  • Change: Update plugin framework to 046
    • 046:
    • Fix reset_options() to reference instance variable $options.
    • Note compatibility through WP 4.7+.
    • Update copyright date (2017)
    • 045:
    • Ensure reset_options() resets values saved in the database.
    • 044:
    • Add reset_caches() to clear caches and memoized data. Use it in reset_options() and verify_config().
    • Add verify_options() with logic extracted from verify_config() for initializing default option attributes.
    • Add add_option() to add a new option to the plugin’s configuration.
    • Add filter ‘sanitized_option_names’ to allow modifying the list of whitelisted option names.
    • Change: Refactor get_option_names().
    • 043:
    • Disregard invalid lines supplied as part of hash option value.
    • 042:
    • Update disable_update_check() to check for HTTP and HTTPS for plugin update check API URL.
    • Translate “Donate” in footer message.
  • Change: Update unit test bootstrap
    • Default WP_TESTS_DIR to /tmp/wordpress-tests-lib rather than erroring out if not defined via environment variable
    • Enable more error output for unit tests
  • Change: Align config array elements
  • Change: Note compatibility through WP 4.9+
  • Change: Remove support for WordPress older than 4.6
  • Change: Update copyright date (2018)

Full changelog is available in CHANGELOG.md.


Screenshots
FAQ
ChangeLog