Sometimes, when creating a custom post type, we are needed to load some specific scripts/styles on Custom Post ‘Edit’ section on the WordPress dashboard. And you want to load these scripts/styles only for that particular custom post type and not for others.

Below is the code snippet which will assist you to achieve this. Put this code inside the functions.php file.

/**
 * Snippet Name: Add admin script on custom post types
 */
function cdxn_load_admin_cpt_script( $hook ) {
    global $post;  
    if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
        if ( 'testimonial' === $post->post_type ) {   
            wp_enqueue_style( 'mystyle_css', get_template_directory_uri() . '/css/admin_test.css',false,'1.1','all');
            wp_enqueue_script( 'myscript_js', get_stylesheet_directory_uri().'/js/my_testimonial_admin_script.js' );
        }
    }
  }
add_action( 'admin_enqueue_scripts', 'cdxn_load_admin_cpt_script', 10, 1 );

Here your custom post type name is ‘testimonial’. You might need to use your own path (the path where you have kept your JS/CSS files) for ‘wp_enqueue_style’ and ‘wp_enqueue_script’ functions.

Thank you for reading this article! If you would like to know how to exclude custom post type from WordPress search result page, I would suggest to read this article.

You are also welcome to visit our other blog posts on WordPress from here