Claire
Claire

Reputation: 3775

How to use javascript on backend of wordpress

I want to register a script with wordpress that will work on the backend. I thought any script I registered in my functions.php file would work but it only shows in the front end. Anyone any ideas how to do this?

Upvotes: 2

Views: 1510

Answers (2)

iambriansreed
iambriansreed

Reputation: 22251

Try:

<?php

add_action( 'admin_head', 'myscriptinthe_admin_header' );
function myscriptinthe_admin_header(){
  echo "<script>alert( 'in the admin head!' )</script>";
}

add_action( 'admin_head', function(){ ?>    
<script> alert( 'in the admin head!' ) </script> 
<?php }); ?>

Upvotes: 2

datasn.io
datasn.io

Reputation: 12867

In addition to the answer above, sometimes we may need to add javascript to the footer of WordPress admin backend instead of in the head.

In this case, we will use the 'admin_footer' hook instead of 'admin_head'.

Upvotes: 1

Related Questions