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 );
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 );
Share
Improve this question
edited Feb 18, 2011 at 7:38
Noel Tock
asked Feb 17, 2011 at 16:45
Noel TockNoel Tock
8812 gold badges8 silver badges21 bronze badges
2
|
3 Answers
Reset to default 3Ok, 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 )
return;
// 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... :)
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");
}
Update
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.
I tried previous solutions that did not work on the edit page I got this code working fine on the edit page
global $typenow;
if ( $typenow == 'slider' || get_post_type($_GET['post']) == 'slider' ):
// What you want to do
endif;
admin_init
is appropriate for what this function does? (enqueues can go onto better appropriated hooks, if that's what you're doing).. – t31os Commented Feb 17, 2011 at 17:52