2
votes

I have this little strange problem ;). I would like to load custom scripts (css, js) only when an admin's custom post type editor is loaded (adding new custom post type, editing custo mpost type).

Custom post types are registered this way:

add_action( 'init', 'cs_product_register_post_types' );
function cs_product_register_post_types() {
    $product_args = array(
        'public' => true,
        'rewrite' => array(
            'slug' => 'products',
            'with_front' => false,
            'pages' => false
        ),
        'supports' => array(
            'title',
            'editor',
            'page-attributes'
        ),
        'labels' => array(
            'name' => 'Produkty',
            'singular_name' => 'Produkt',
            'add_new' => 'Dodaj nowy produkt',
            'add_new_item' => 'Dodaj nowy produkt',
            'edit_item' => 'Edytuj produkt',
            'new_item' => 'Nowy produkt',
            'view_item' => 'Wyswietl produkt',
            'search_items' => 'Wyszukaj produkt',
            'not_found' => 'Produktu nie znaleziono',
            'not_found_in_trash' => 'Brak usuniętych produktów'
        ),
        'menu_position' => 3,
    );
    register_post_type( 'products', $product_args );
}

For those there is a function registering custom metabox:

add_action( 'add_meta_boxes', 'cs_products_mb_create' );
function cs_products_mb_create() {
    //create a custom meta box
    add_meta_box( 'products-info', 'Ustawienia Produktu', 'cs_products_mb_function', 'products', 'normal', 'high' );
}

That works only for registered custom post type (products).

Now the only thing is to load custom js. It can be done this way:

add_action('admin_print_styles-post.php', 'cs_products_admin_styles');
add_action('admin_print_styles-post-new.php', 'cs_products_admin_styles');

function cs_products_admin_styles() {
    wp_enqueue_style( 'thickbox' );
    wp_enqueue_style ('theme', get_bloginfo('template_url') . '/css/admin.css', '', '1.0');
} 

But it works for all posts, and its not a best way to do it ;).

Thanks for any ideas.


edit

After digging, and digging, and digging... One of simplest ways to do it:

//load scripts only when on products custom post type edit page
if ( ( isset($_GET['post_type']) && $_GET['post_type'] == 'products' ) || ( isset($post_type) && $post_type == 'products' ) ) {
    add_action('admin_enqueue_scripts', 'cs_admin_customposttype_scripts');
}

function cs_admin_customposttype_scripts(){ 
    wp_enqueue_style ('theme', get_bloginfo('template_url') . '/css/admin.css', '');    
    wp_enqueue_script( 'cs-image-upload', get_bloginfo('template_url').'/js/admin.js', array( 'jquery') );
}

But the problem with this solution is, that it works only when new custom post is being created. When editing, $_GET['post_type'] or $post_type are not available.

1

1 Answers

1
votes

After searching a little bit more...

//load scripts only when on products custom post type edit page
if ( ( isset($_GET['post_type']) && $_GET['post_type'] == 'products' ) || ( isset($post_type) && $post_type == 'products' ) || ( isset($_GET['post']) && get_post_type($_GET['post']) == 'products' ) ) {
    add_action('admin_enqueue_scripts', 'cs_admin_customposttype_scripts');
}

function cs_admin_customposttype_scripts(){ 
    wp_enqueue_style ('theme', get_bloginfo('template_url') . '/css/admin.css', '');    
    wp_enqueue_script( 'cs-image-upload', get_bloginfo('template_url').'/js/admin.js', array( 'jquery') );
}

Aditional or, and use of only one known variable - postID that is sent with $_GET and using get_post_type function did the job.