Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
FYI: Enqueues should not go on init.. (lots of people do it, but it's not the right place for an enqueue).. –  t31os Nov 29 '11 at 15:40
add comment

2 Answers

up vote 17 down vote accepted

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' );
}
share|improve this answer
    
it works like a charm! thx so much!!! –  frabiacca Feb 12 '12 at 0:44
add comment
//Enter custom JS TO ADMIN AREA

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

function banner_admin_script() {
    global $post_type;
    if( $post_type == 'banner' )
      wp_enqueue_script( 'portfolio-admin-script', plugins_url( '/js/admin.js', __FILE__),'','', true); //"TRUE"-ADDS JS TO FOOTER

}

I made some changes in this code to work for me:

  1. I change get_stylesheet_directory_uri() . '/admin.js' with plugins_url( '/js/admin.js', __FILE__), this was needed because I've developed a plugin for banner which is the better solution instead creating CPT inside functions.php

  2. I've added - "true" to send the code at the footer area instead of the head - improves time for loading

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.