WordPress Snippets at WPcustoms

Add admin script on custom post types

if you require a specific javascript file enqueued only on pages related to your custom post type (in this example MOVIES) use this function.


/**
 * Snippet Name: Add admin script on custom post types
 * Snippet URL: http://www.wpcustoms.net/snippets/add-admin-script-on-custom-post-types/
 */
 function wpc_add_admin_cpt_script( $hook ) {

    global $post;

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