I have created a custom post type “portfolio” with something like this :
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
//'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 4,
'taxonomies' => array('post_tag','category'),
'supports' => array('title','editor','comments','trackbacks','revisions','custom-fields','page-attributes','thumbnail', 'excerpt', 'tags')
);
register_post_type( 'portfolio' , $args );
And I have some other custom fields in there with an action :
add_action("admin_init", "admin_init");
function admin_init(){ // add_meta_box( $id, $title, $callback, $page, $context, $priority );
add_meta_box("media", "Media Type", "media", "portfolio", "side", "high");
add_meta_box("map_meta", "Mapping Info", "map_meta", "portfolio", "normal", "high");
}
Although I once had this working I can not figure out to get it to load scripts just for this page. Right now I just have them in with the rest of the wp_enqueue_script
like this :
function my_init() {
if (!is_admin()) {
....
}
if (is_admin()) {
wp_register_script('Gmaps', 'http://maps.google.com/maps/api/js?sensor=false', false, '3.0', false);
wp_enqueue_script('Gmaps');
wp_register_style('admin_js', get_bloginfo('template_directory') . '/admin.js');
wp_enqueue_script('admin_js');
wp_register_script('Zmaps', get_bloginfo('template_directory') .'/scripts/maps.js', array('Gmaps'), '1.0', true);
wp_enqueue_script('Zmaps');
}
}
add_action('wp_enqueue_scripts', 'my_init');
But none of this is loading for me. How can I load these scripts into the admin pages? Better yet how can I load them specifically for the edit pages of the portfolio custom post type?
Try this code for adding scripts to the edit pages of your portfolio custom post type.
I’ll post a better solution because the accepted answer is old and does not use the right hooks.
First of all: To enqueue scripts and styles in admin area, must be used
admin_enqueue_scripts
and nothing else.Second: Forget any global vars. Use the current screen object to perform different checks.
Here is a ready copy paste code:
Note: Replace
'portfolio'
with the needed post type slug.I made some changes in this code to work for me:
I changed
get_stylesheet_directory_uri() . '/admin.js'
toplugins_url( '/js/admin.js', __FILE__),
– this was needed because I’ve developed a plugin for a banner, which is the better solution instead creating CPT insidefunctions.php
I’ve added “
true
” to send the code at the footer area instead of the head – improves time for loading