60

I want to add some custom jquery code to the Edit Post page, something really simple like showing a div when someone presses Publish.

The only restriction is that I want to achieve this through the use of a plugin, not hacking the admin template files.

I've tried echoing some script tags using some actions but it doesn't seem to be the way.

Fedir RYKHTIK
  • 9,844
  • 6
  • 58
  • 68
Ignacio
  • 7,947
  • 15
  • 63
  • 74

8 Answers8

110

Use the admin_enqueue_scripts action and the wp_enqueue_script method to add custom scripts to the admin interface.

This assumes that you have myscript.js in your plugin folder. Change accordingly. The my_custom_script handle should be unique for your module and script.

function my_enqueue($hook) {
    // Only add to the edit.php admin page.
    // See WP docs.
    if ('edit.php' !== $hook) {
        return;
    }
    wp_enqueue_script('my_custom_script', plugin_dir_url(__FILE__) . '/myscript.js');
}

add_action('admin_enqueue_scripts', 'my_enqueue');
Tim
  • 6,281
  • 3
  • 39
  • 49
  • 4
    This is the correct answer for the current version of WordPress (the example is pulled directly from the WordPress Codex). – thirdender Mar 04 '14 at 23:20
  • 2
    What file should I place this code in? Where in the file should this code be placed? – commadelimited Dec 17 '16 at 20:15
  • I was trying this as well but couldn't get it working (or didn't have time to fuss w/it long enough). Fortunately this plugin did what I needed: Add Admin Javascript: https://wordpress.org/plugins/add-admin-javascript/ – Christopher Mar 30 '17 at 10:57
  • Why did you put that condition with 'edit.php'? What does it mean? @Tim – montie May 27 '18 at 17:57
  • @montie It only adds the script to the `edit.php` admin page. See the [docs](https://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts). – Tim May 28 '18 at 06:41
  • @Tim Okay, so, we need to add this code into plugin file.. but if the plugin is updated this code will be removed no ? – Dev'Hamz Oct 18 '18 at 16:06
  • A bit explanation: The admin_enqueue_scripts hooks allow us to pass a variable called $hook to our my_enqueue() function. This $hook will contain the name, or hook, of the current admin page, which we can then use to conditionally load our scripts. For this example, let’s assume that we want to load some extra JS only into the edit.php page, which is the primary page/file loaded when viewing a list of any post type, such as the Posts or Pages page. – Motahar Hossain Dec 11 '19 at 00:39
  • Sorry for being picky, but why do we need the type check in "if ('edit.php' !== $hook) {"? Wouldn't "!=" be enough? – Alexandre Schmidt Jan 15 '20 at 13:23
  • 1
    @AlexandreSchmidt See this excellent answer: https://stackoverflow.com/a/40392064/698511 – Tim Jan 16 '20 at 09:53
60

There is a snippet for Your functions.php file :

function custom_admin_js() {
    $url = get_bloginfo('template_directory') . '/js/wp-admin.js';
    echo '"<script type="text/javascript" src="'. $url . '"></script>"';
}
add_action('admin_footer', 'custom_admin_js');

Works fine on Wordpress 3.2.1.

forsvunnet
  • 1,238
  • 10
  • 17
Fedir RYKHTIK
  • 9,844
  • 6
  • 58
  • 68
  • I stuck this in my theme so that I could load some jQuery code to hide some parts of the admin bar that I didn't want my client messing with, screwing up the website. See, I'm using the PODS Plugin for content and a custom theme. – Volomike Apr 18 '12 at 04:21
  • @KuldeepDaftary Probably You could activate Your custom JS via a function, which will be triggered after page and jQuery loading ? – Fedir RYKHTIK May 24 '13 at 11:46
  • 3
    This is the wrong way to add a script. You should enqueue the script, as shown in [Tim's answer](http://stackoverflow.com/a/21914399/1924128). – J.D. Jun 30 '15 at 19:45
  • Great example, I like to just remove `'. $url .'` altogether and write the JS directly within, saves loading another JS file in the Admin area. – Nathan May 28 '19 at 19:07
21
<?php
function add_jquery_data() {
    global $parent_file;

    if ( isset( $_GET['action'] ) && $_GET['action'] == 'edit' && isset( $_GET['post'] ) && $parent_file == 'edit.php') {
    // Do some stuff.
    }
}

add_filter('admin_head', 'add_jquery_data');

?>
Sean Fisher
  • 625
  • 5
  • 13
  • Thanks a bunch, this is exactly what I've been looking for. I've used admin_footer anyways. However, is there a way to load the content _only_ on new post/edit post? – Ignacio Jul 24 '10 at 21:48
  • 4
    Well if you want it for the new post page as well, you can try this: – Sean Fisher Jul 24 '10 at 22:29
7

admin_enqueue_scripts and wp_enqueue_script are the preferred way to add javascript files to the dashboard.

// I'm using an anonymous function for brevity.
add_action( 'admin_enqueue_scripts', function() {
    wp_enqueue_script( 'handle', plugin_dir_url( __FILE__ ) . '/script.js' );
} );

If you want to output the javascript using your PHP function however, wp_add_inline_script doesn't seem to work. Instead, you can use admin_print_scripts to directly echo out the script, including the script tags themselves. Just ensure to set the priority high so that it loads after any required libraries, such as jQuery.

add_action( 'admin_print_scripts', function() {
    // I'm using NOWDOC notation to allow line breaks and unescaped quotation marks.
    echo <<<'EOT'
<script type="text/javascript">
jQuery(function($){
    // Do stuff here.
});
</script>
EOT;
}, PHP_INT_MAX );
Shaun Cockerill
  • 800
  • 8
  • 11
1

If you want to get fancy and filter where you want to load the file or not, best to use get_current_screen.

function myproject_admin_enqueue() {

    $screen = get_current_screen();

    // load on the NEW and EDIT screens of all post types
    if ( 'post' === $screen->base ) {
        wp_enqueue_script('my_custom_script', plugin_dir_url( __FILE__ ) . 'all-admin.js');
    }

    // load on the NEW and EDIT screens of one post type
    if ( 'post' === $screen->base && 'myposttype' === $screen->post_type ) {
        wp_enqueue_script('my_custom_script', plugin_dir_url( __FILE__ ) . 'mypostype-admin.js');
    }

    // load only when adding a NEW post, not when editing an existing one.
    if ( 'post' === $screen->base && 'add' === $screen->action ) {
        wp_enqueue_script('my_custom_script', plugin_dir_url( __FILE__ ) . 'new-admin.js');
    }

}
add_action('admin_enqueue_scripts', 'myproject_admin_enqueue');
squarecandy
  • 4,894
  • 3
  • 34
  • 45
1

Directly adding wp_enqueue_script to your code doesn't include the script in new versions of Wordpress (5.0 above). The better way is to register the script with wp_register_script first to create a handle and then enqueue that handle.

function custom_script_in_admin($hook) {
    if ('edit.php' !== $hook) {
        return;
    }
    wp_register_script( 'custom_handle_name',plugin_dir_url( __FILE__ ) . '/script.js', '',true );
    wp_enqueue_script('custom_handle_name');
}

add_action('admin_enqueue_scripts', 'custom_script_in_admin');
tosturaw
  • 7
  • 3
0

Somehow the accepted answer didn't work for me. Here is another solution I didn't found in the answers and this is what did it for me, WP 5.x, adding some css and js for my backend...

function suedsicht_theme_add_editor_assets() {
  wp_enqueue_style( 'custom-gutenberg-stylesheet', get_template_directory_uri() . '/css/custom-editor-style.css', array(), wp_get_theme()->get( 'Version' ), 'all' );
  wp_enqueue_script('suedsicht-admin-js', get_template_directory_uri() . '/js/admin.js');
}
add_action( 'enqueue_block_editor_assets', 'suedsicht_theme_add_editor_assets' );
chakmear
  • 101
  • 5
0

By using the admin enqueue script hook you can easily add the script file for the admin panel.

function custom_admin_js() {

wp_enqueue_script( 'custom_wp_admin_js', get_template_directory_uri() . '/new-assets/js/admin_section.js', false, '1.0.0' );
wp_enqueue_script( 'custom_wp_admin_js' );
}

add_action('admin_enqueue_scripts', 'custom_admin_js');
Sachin Saini
  • 359
  • 2
  • 10