So I continue to run into this issue and I just looking for the best and simplest solution to solve this problem.
I have come to make use of custom post types in many different projects and have extended these with custom metaboxes which I have then further extended by adding custom scripts such as jQuery event calendar selectors… All of this works great except for one key issue… I don’t want these custom jQuery scripts to load on every page in the admin.
I am essentially just looking for a way to just have these custom jquery fields loaded when I am on the “edit post” page for a SPECIFIC post type.
What’s the best solution here?
UPDATE 1
First of all, thank you very much.
I am actually shocked that plugin developers dont make sure of things like this because as I am finding out this is one of the key reasons that problems exist with different plugins.
I am having some further issues though with this. For example…
I have modified the script to call the if statement like this:
if (is_admin() && $pagenow=='post-new.php' OR $pagenow=='post.php' && $typenow=='events')
As you can see I am trying to set things up so that my scripts ONLY get called when I am adding or editing a post within the post type of “events”.
I don’t want the script to load on any other page and also don’t want it to run on the list of page within the post type of “events” so I would think the if statement is correct.
The problem however seems to be that the script only gets loaded when I create a new post within this post type but it does not seem to work when I am editing an existing post.
Could you test this out and maybe let me know what I could be doing wrong?
Here is the exact code which I am using… maybe there is a better or simple way of doing this?
<?php
// INCLUDE METABOX CUSTOM JQUERY DATEPICKER 2
add_action('admin_init','load_admin_datapicker_script');
function load_admin_datapicker_script() {
global $pagenow, $typenow;
if (is_admin() && $pagenow=='post-new.php' OR $pagenow=='post.php' && $typenow=='events') {
$ss_url = get_bloginfo('stylesheet_directory');
wp_enqueue_script('jquery');
wp_enqueue_script('custom_js_jquery_ui',"{$ss_url}/admin-metabox/js/jquery-ui-1.7.1.custom.min.js",array('jquery'));
wp_enqueue_script('custom_js_daterangepicker',"{$ss_url}/admin-metabox/js/daterangepicker.jQuery.js",array('jquery'));
wp_enqueue_script('custom_js_custom',"{$ss_url}/admin-metabox/js/custom.js",array('jquery'),NULL,TRUE);
wp_enqueue_style('custom_css_daterangepicker',"{$ss_url}/admin-metabox/css/ui.daterangepicker.css");
wp_enqueue_style('custom_css_jquery_ui',"{$ss_url}/admin-metabox/css/redmond/jquery-ui-1.7.1.custom.css");
}
}
Also… if I wanted to add three post types and load different JS scripts for each post types then would I just duplicate the code above three separate times or is this not a good way of doing this? For example… would it be better to just call:
global $pagenow, $typenow;
At the top of my functions file or does it matter or complicate things when I duplicate it more than once?
On a different problem related to the same… I for example am utilizing the “gravity forms” plugin but I have noticed their scripts run on every page on the admin which is causing issues with other plugins. How would i go about modifying their script to ensure the scripts only get loaded when I need them.
UPDATE 2
I have modified my functions.php file with the code provided by Mike (below) however it seems that the applicable javascript is still being included when you create a NEW Post or Page. This means that when you attempt to create a NEW Post or Page either by creating a new default wordpress post/page or when you create a NEW post/page based off one of your custom post types. The code proposed by Mike IS working on all other admin pages and it DOES work when you “EDIT” an existing post/page or custom post type. Any suggested modifications to make this work correct?
Here is my current code:
<?php
add_action('admin_init','load_admin_datapicker_script');
function load_admin_datapicker_script() {
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') {
$ss_url = get_bloginfo('stylesheet_directory');
wp_enqueue_script('jquery');
wp_enqueue_script('custom_js_jquery_ui',"{$ss_url}/admin-metabox/js/jquery-ui-1.7.1.custom.min.js",array('jquery'));
wp_enqueue_script('custom_js_daterangepicker',"{$ss_url}/admin-metabox/js/daterangepicker.jQuery.js",array('jquery'));
wp_enqueue_script('custom_js_custom',"{$ss_url}/admin-metabox/js/custom.js",array('jquery'),NULL,TRUE);
wp_enqueue_style('custom_css_daterangepicker',"{$ss_url}/admin-metabox/css/ui.daterangepicker.css");
wp_enqueue_style('custom_css_jquery_ui',"{$ss_url}/admin-metabox/css/redmond/jquery-ui-1.7.1.custom.css");
}
}
?>