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:
/post-new.php?post_type=events
But for editing a post it doesn’t work, where the URL is:
/post.php?post=12&action=edit
(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 )
return;
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 )
return;
wp_deregister_script('jquery-ui-core');
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 );
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..
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..
If you’re loading a script, then you might use..
If you’re loading a stylesheet, then possibly..
Else i’ll need to know more about what you’re doing… 🙂
you can try to call on global $post so:
Update
try :
also make sure your register_post_type call runs first so the post type is registered before this code is run.
Hope this helps.
I tried previous solutions that did not work on the edit page
I got this code working fine on the edit page