Using WordPress ‘enqueue_comment_hotkeys_js()’ PHP function

The enqueue_comment_hotkeys_js() WordPress PHP function is used to enqueue the jQuery script that enables comment shortcuts.

Usage

To use the enqueue_comment_hotkeys_js() function, you simply need to call it in your PHP code. Since this function does not accept any parameters, it can be invoked as is:

enqueue_comment_hotkeys_js();

Parameters

  • This function does not accept any parameters.

More information

See WordPress Developer Resources: enqueue_comment_hotkeys_js()

Please note that the enqueue_comment_hotkeys_js() function was introduced in WordPress version 3.2.0. As of the last update, there’s no information about it being depreciated. Related functions include wp_enqueue_script() and wp_enqueue_style().

Examples

Enabling Comment Shortcuts

If you want to enable comment shortcuts in your WordPress admin, simply call the function in your theme’s functions.php file.

add_action('admin_print_scripts', 'enqueue_comment_hotkeys_js');

This code hooks the enqueue_comment_hotkeys_js() function to the admin_print_scripts action, enabling the comment shortcuts.

Conditional Enqueueing

You might want to enqueue the script only on specific pages. In this case, use the enqueue_comment_hotkeys_js() function within a conditional:

function my_custom_function() {
  if (is_single()) {
    enqueue_comment_hotkeys_js();
  }
}
add_action('admin_print_scripts', 'my_custom_function');

This code will only enqueue the comment shortcuts script on single post pages.

Disabling Comment Shortcuts

To disable comment shortcuts, you can dequeue the script using the wp_dequeue_script() function:

function my_custom_function() {
  wp_dequeue_script('admin-comments');
}
add_action('admin_print_scripts', 'my_custom_function');

This code dequeues the comment shortcuts script, effectively disabling it.

Checking if the Script is Enqueued

You can check whether the comment shortcuts script is enqueued using the wp_script_is() function:

if (wp_script_is('admin-comments', 'enqueued')) {
  echo "The comment shortcuts script is enqueued.";
}

This code checks if the comment shortcuts script is enqueued and echoes a message if it is.

Conditionally Disabling Comment Shortcuts

You might want to disable comment shortcuts on specific pages. In this case, dequeue the script within a conditional:

function my_custom_function() {
  if (is_page('contact')) {
    wp_dequeue_script('admin-comments');
  }
}
add_action('admin_print_scripts', 'my_custom_function');

This code dequeues the comment shortcuts script on the ‘contact’ page.