How to enqueue scripts on custom post add/edit pages?

I’m trying to enqueue a JS script only when someone is adding or editing a custom post type I created called “recipes”. Currently the script works ok when I do this:

if (is_admin()){
    wp_enqueue_script( 'my-script' );

But this loads it in every admin page, I’m assuming I need to hook it to a function but I hvae no idea what it’d be.

Thanks in advance!


You can do it like this (put in your functions.php) :

function add_admin_scripts( $hook ) {

    global $post;

    if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
        if ( 'recipes' === $post->post_type ) {     
            wp_enqueue_script(  'myscript', get_stylesheet_directory_uri().'/js/myscript.js' );
add_action( 'admin_enqueue_scripts', 'add_admin_scripts', 10, 1 );

Leave a Comment