Load a script just to custom post type in admin

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?

3

Try this code for adding scripts to the edit pages of your portfolio custom post type.

add_action( 'admin_print_scripts-post-new.php', 'portfolio_admin_script', 11 );
add_action( 'admin_print_scripts-post.php', 'portfolio_admin_script', 11 );

function portfolio_admin_script() {
    global $post_type;
    if( 'portfolio' == $post_type )
    wp_enqueue_script( 'portfolio-admin-script', get_stylesheet_directory_uri() . '/admin.js' );
}

Leave a Comment