Loading External Scripts in Admin but ONLY for a Specific Post Type?

First, I assume you are using wp_enqueue_script() to load your scripts, right?

That said, if I understand your question then what you need is something like the following. You’ll have to edit it for your details, of course, but it gives you the general framework.

We are hooking admin_init with the function load_my_script() to tests the global $pagenow for a match with the admin page edit.php, and the global $typenow to see if the post type is the one you want.

The rest are just details which you can read about here if you need to learn more:

<?php
add_action('admin_init','load_my_script');
function load_my_script() {
  global $pagenow, $typenow;
  if ($pagenow=='edit.php' && $typenow=='my-custom-type') {
    $ss_url = get_bloginfo('stylesheet_directory');
    wp_enqueue_script('jquery');
    wp_enqueue_script('my-custom-script',"{$ss_url}/js/my-custom-script.js",array('jquery'));
  }
}

UPDATE

I’m replying to your update. Unfortunately (for whatever reason) $typenow does not have a value during admin_init so you’ll need to get the post_type by loading the post based on the URL parameter 'post' as you see in the follow example (I’ve copied the line above and line below from your example so you can see where to place it):

<?php
global $pagenow, $typenow;
if (empty($typenow) && !empty($_GET['post'])) {
  $post = get_post($_GET['post']);
  $typenow = $post->post_type;
}
if (is_admin() && $pagenow=='post-new.php' OR $pagenow=='post.php' && $typenow=='events') {

P.S. As for your other questions, please post them as new question on the site for me or others to answer. Since we are working so hard to help you, please take great care to give your title the best title you possibly can and also please write your questions as clearly and succinctly as possible with good formatting and proper English. If you’ll do this it will help with the same issues recognize your question as being similar to what they need and it will make it easier on us who are answering your questions.

I ask this of you (and of all others who ask questions on WordPress Answers) as a favor in exchange for taking the time effort to answer your questions because I and the other moderators want to make WordPress Answers a tremendous resource for the community instead of yet another sloppy forum that is hard to read and hard to find answers like so many other sites on the web.

UPDATE #2

I thought you might have had an operator precedence issues in your if statement but when I tested before I didn’t run into it. If it is behaving as your say then you almost certainly do so try this instead (sorry, I don’t have time to test this right now to ensure for certain that it works):

<?php
add_action('admin_init','load_my_script');
function load_my_script() {
  global $pagenow, $typenow;
  if (empty($typenow) && !empty($_GET['post'])) {
    $post = get_post($_GET['post']);
    $typenow = $post->post_type;
  }
  if (is_admin() && $typenow=='events') {
    if ($pagenow=='post-new.php' OR $pagenow=='post.php') { 
      $ss_url = get_bloginfo('stylesheet_directory');
      wp_enqueue_script('jquery');
      wp_enqueue_script('my-custom-script',"{$ss_url}/js/my-custom-script.js",array('jquery'));
    }
  }
}

Leave a Comment