1

I am trying to load several scripts and stylesheets into a plugin I am creating. I want to load scripts into multiple CPTs within admin. I have got this far:

function fhaac_admin_enqueue_scripts(){
    global $pagenow, $typenow;

    if ( ($pagenow == 'post.php' || $pagenow == 'post-new.php') && $typenow == 'fhaac' ){}
}

The scripts are being loaded into the fhaac, but nothing else. I am not sure how to add multiple CPTs. I tried adding them in an array, but it didn't work.

Help would be greatly appreciated.

Cheers

2 Answers 2

1

There is a built-in function that you can use, instead of globals. The get_current_screen() function allows you to get the information associated with the current page.

One of its return values is post_type. So you can check against an array of post types to see if anyone matches.

function fhaac_admin_enqueue_scripts(){
    $screen = get_current_screen();
    if (
        in_array( $screen->post_type, array('fhaac','blabla')) &&
        $screen->base == 'post'
     ) { // Do something }
}
0

There's a really simple work around:

Load it everywhere and only run the code if the page matches

For example, on my site I have a CPT named projects and on the body tag I have the following:

<body
    class="wp-admin wp-core-ui js   jetpack-connected  edit-php auto-fold admin-bar post-type-project branch-4-8 version-4-8 admin-color-fresh locale-en-us multisite customize-support sticky-menu svg">

So I know that my posts listing will match body.edit-php, and body.post-type-project

So for CSS:

body.post-type-project.edit-php .... {
    ....
}

And I can do the same check in javascript be it with jQuery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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