How to add custom javascript to WordPress Admin

0 votes
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.
May 8, 2020 in PHP by kartik
• 37,530 points
1,750 views

1 answer to this question.

0 votes

Hello,

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');

Thank You!!

answered May 8, 2020 by Niroj
• 82,820 points

Related Questions In PHP

0 votes
1 answer

How to get the current plugin directory in WordPress?

Hello @kartik, This will actually get the result ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,820 points
909 views
0 votes
1 answer

How to connect to SQL Server database from JavaScript in the browser?

Hello @kartik, You shouldn´t use client javascript to ...READ MORE

answered Jul 21, 2020 in PHP by Niroj
• 82,820 points
9,357 views
0 votes
1 answer

How to add slug to href?

Hello @aakash, You are not sending the variable ...READ MORE

answered Aug 11, 2020 in PHP by Niroj
• 82,820 points
204 views
0 votes
1 answer

How to get WordPress post featured image URL?

Hello @kartik, Try this code below: <?php if (has_post_thumbnail( $post->ID ...READ MORE

answered Aug 14, 2020 in PHP by Niroj
• 82,820 points
1,138 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
9,851 views
0 votes
1 answer

What is type casting and type juggling in php?

Hey, The way by which PHP can assign ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,820 points
1,349 views
0 votes
2 answers

How can we create a session in PHP?

Hello, niroj. Here is my idea session_start(); $_SESSION['USERNAME'] ...READ MORE

answered Dec 7, 2020 in PHP by Famous
• 140 points
254 views
0 votes
1 answer

What is the use of $_REQUEST variable in php?

Hii @kartik, The $_REQUEST variable is used to read the ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,820 points
459 views
0 votes
1 answer

How can I add PHP page to WordPress?

Hii @kartik, First, duplicate post.php or page.php in your theme folder. Rename the ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,820 points
418 views
0 votes
1 answer

How to load Javascript in Wordpress Plugin?

Hello kartik, You need to specify when the ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,820 points
153 views