WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm probably missing something unless it's really not possible, for some reason I can't get my function to only execute on the new and edit page of a custom post type (the new not being a problem):

I'm currently using:

if ((isset($_GET['post_type']) && $_GET['post_type'] == 'events') || (isset($post_type) && $post_type == 'tf_events')) {
    add_action('admin_init', "admin_head");

However this only works for adding a new post where the URL is:


But for editing a post it doesn't work, where the URL is:


(although the post type isn't added to the link attribute)

WORKING CODE (See Mark's response below):

// 7. JS Datepicker UI

function events_styles() {
    global $post_type;
    if( 'tf_events' != $post_type )
    wp_enqueue_style('ui-datepicker', get_bloginfo('template_url') . '/css/jquery-ui-1.8.9.custom.css');

function events_scripts() {
    global $post_type;
    if( 'tf_events' != $post_type )
    wp_enqueue_script('jquery-ui', get_bloginfo('template_url') . '/js/jquery-ui-1.8.9.custom.min.js', array('jquery'));
    wp_enqueue_script('ui-datepicker', get_bloginfo('template_url') . '/js/jquery.ui.datepicker.min.js');
    wp_enqueue_script('custom_script', get_bloginfo('template_url').'/js/pubforce-admin.js', array('jquery'));

add_action( 'admin_print_styles-post.php', 'events_styles', 1000 );
add_action( 'admin_print_styles-post-new.php', 'events_styles', 1000 );

add_action( 'admin_print_scripts-post.php', 'events_scripts', 1000 );
add_action( 'admin_print_scripts-post-new.php', 'events_scripts', 1000 );
share|improve this question
Some clarification on what this function does would go a long way in helping, are you certain admin_init is appropriate for what this function does? (enqueues can go onto better appropriated hooks, if that's what you're doing).. – t31os Feb 17 '11 at 17:52
Definitely not certain :) – Noel Tock Feb 17 '11 at 19:15
up vote 3 down vote accepted

Ok, total revision on my original answer, which just turned into a mess.

The issue with your code is that you're firing on init, this covers every admin page, and additionally it's too early to check admin vars to work out the current page(though you could just check $_GET if you really need to run code that early).

You want to specifically hook to the post edit pages, though you've not indicated you need to run on the post listing to(edit.php), so i'll exclude that from the examples that follow.

You can hook to a few different actions that occur inside the admin head, and do whatever you need to do there and be able to reliably check the post type by giving $post_type scope inside your callback function.

So your callback would go a little something like this..

function mycallback() {
    global $post_type;
    if( 'events' != $post_type )
    // Do your code here

What action you want that hooked onto is down to what you want to run at that point, if you're not doing anything in particular but want to execute something, then perhaps the generic admin head hook will be suitable..

add_action( 'admin_head-post.php', 'mycallback', 1000 );
add_action( 'admin_head-post-new.php', 'mycallback', 1000 );

If you're loading a script, then you might use..

add_action( 'admin_print_scripts-post.php', 'mycallback', 1000 );
add_action( 'admin_print_scripts-post-new.php', 'mycallback', 1000 );

If you're loading a stylesheet, then possibly..

add_action( 'admin_print_styles-post.php', 'mycallback', 1000 );
add_action( 'admin_print_styles-post-new.php', 'mycallback', 1000 );

Else i'll need to know more about what you're doing... :)

share|improve this answer
lol, just updated my code to use global $typenow; – Bainternet Feb 17 '11 at 17:46
Just note, it doesn't always get populated for the post post type, it's inconsistently set on some pages, but behaviour is consistent for other post types, including pages(that's why i have the empty condition in my sample). – t31os Feb 17 '11 at 17:50
...further, you should never need to reference $_GET in the admin unless you're passing around your own custom query vars. – t31os Feb 17 '11 at 17:54
Thank you gents, going to play with it now! – Noel Tock Feb 17 '11 at 19:19
Thank you for the help, also no luck here. It works with the add new page, but not the edit. To confirm, I'm also registering the post type within the same file but above this code, CPT & Tax are being loaded via 'init' and I've tested the code above with 'admin_init'. It's also getting far enough within the code to display on add new like said, stumped tbh :/ – Noel Tock Feb 17 '11 at 19:25

you can try to call on global $post so:

global $post;
if ((isset($_GET['post_type']) && $_GET['post_type'] == 'events') || (isset($post_type) && $post_type == 'tf_events') || (get_post_type($post) == "events") || (get_post_type($post) == "tf_events")){) {
    add_action('admin_init', "admin_head");


try :

global $typenow;
if( 'your_post_type' == $typenow ){
// do you stuff

also make sure your register_post_type call runs first so the post type is registered before this code is run.

Hope this helps.

share|improve this answer
Thanks for giving it a shot, no dice unfortunately. – Noel Tock Feb 17 '11 at 17:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.