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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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' );
}
Method 2
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:
<?php
function wpse_cpt_enqueue( $hook_suffix ){
$cpt = 'portfolio';
if( in_array($hook_suffix, array('post.php', 'post-new.php') ) ){
$screen = get_current_screen();
if( is_object( $screen ) && $cpt == $screen->post_type ){
// Register, enqueue scripts and styles here
}
}
}
add_action( 'admin_enqueue_scripts', 'wpse_cpt_enqueue');
Note: Replace 'portfolio' with the needed post type slug.
Method 3
// 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:
-
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0