How to Add Plugin Scripts and CSS to a Specific Custom Post Type

I am working on a new calendar plugin for WordPress and discovered that my jQuery scripts and CSS were loading on every page in the admin. Whoops!

The solution was to include my scripts in the following manner:

// Only load the scripts and css on the a specific custom post type in the admin
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=='add_your_custum_post_type_name_here') {
    if ($pagenow=='post-new.php' OR $pagenow=='post.php') { 
      require ( dirname(__FILE__) .'/add_your_scripts_loader_file_here.php' );
    }
  }
}

I have discovered over the years that many plugins fail to include their scripts in this manner, which can cause all sorts of weird things to happen in the admin.

Enjoy!

Thanks to Mike for the answer.

The Mighty Mo! Design Co.

RSS From Toby’s Blog

  • Where uncertainty exists, don’t order specific.
    4 Idiot Case Studies with 2 simple age-old business lessons. The post Where uncertainty exists, don’t order specific. appeared first on Toby Cryns, WordPress Trainer in Minneapolis, MN.
  • “Now THAT was a good meeting!”
    A single effective meeting can change the course of history for a company. Imagine if you could lead effective meetings EVERY DAY! You can, but it takes curiosity & practice. This article is a good place to start your journey to a more effective you. The post “Now THAT was a good meeting!” appeared first […]
  • Never Respond to RFPs & Custom Proposal Requests
    Y'all who work at normal 9-5 jobs don't need to deal with the B.S. that is an RFP process (or rather you get paid to deal with it). But us freelancers gotta deal with a whole big bag of schtuff when it comes to custom proposals. Here's my quick-and-dirty RFP filtering process that I use […]

More posts from themightymo.com

Adobe CS4 Flash MN Presentation at FlashMN

By | October 14, 2008

Below are my notes from tonight’s FlashMN meeting starring Dustin Tauer.  The presentation focused on the new features of Adobe Creative Suite 4. Illustrator CS4 Can create multiple art boards.  So, if I wanted to use artwork for the front and back of t-shirt, I would create 2 art boards (up to 100).  There are…

SPECIAL OFFER: $99 WordPress Blog!

By | July 19, 2008

For the Month of July only! Get your very own WordPress blog or static website – $99! This includes: – Domain registration for one year – Hosting for one year – Installation of your WordPress blog – Installation of SEO plugin, Akismet spam prevention, and up to 5 additional plug-ins of your choosing – Online…

Jeff Dunn Photography

By | May 30, 2008

We are pleased to release our newest Flash design for Jeff Dunn Photography! Working with our friend, Nick Bilyk, we designed and created a one-of-a-kind web experience for well-known photographer, Jeff Dunn. This new online experience features stunning transitions and accents that place Jeff’s photography front-and-center, right where it should be. It also features a…

RSS From Toby’s Blog

  • Where uncertainty exists, don’t order specific.
    4 Idiot Case Studies with 2 simple age-old business lessons. The post Where uncertainty exists, don’t order specific. appeared first on Toby Cryns, WordPress Trainer in Minneapolis, MN.
  • “Now THAT was a good meeting!”
    A single effective meeting can change the course of history for a company. Imagine if you could lead effective meetings EVERY DAY! You can, but it takes curiosity & practice. This article is a good place to start your journey to a more effective you. The post “Now THAT was a good meeting!” appeared first […]
  • Never Respond to RFPs & Custom Proposal Requests
    Y'all who work at normal 9-5 jobs don't need to deal with the B.S. that is an RFP process (or rather you get paid to deal with it). But us freelancers gotta deal with a whole big bag of schtuff when it comes to custom proposals. Here's my quick-and-dirty RFP filtering process that I use […]