the code is executed when post type is post or page
No, only if the post type is page (because of the 'page' == $screen->id).
Because that if statement actually checks if the current admin screen is the one for editing or creating a Page (post type of page), where the URL looks like so: (note that the post type is not visible in the URL when a post/Page/CPT is being edited)
http://example.com/wp-admin/post.php?post=2&action=edit
-
On such screens — editing or creating a post/Page/CPT,
$screen->idis equivalent to$screen->post_typewhich is the post type (slug). -
And
$screen->baseispostwhich is the base name of the file name in the current URL — base name is the file name without the extension — so if the file name ispost.php, the base name ispost. However, when creating a post/Page/CPT, the file name is actuallypost-new.php, but still the$screen->baseispost(and notpost-new) because WordPress strips the-newpart. -
Additionally, you can distinguish between editing and creating by checking the screen’s action; i.e. whether
$screen->actioniseditoradd(e.g.'edit' == $screen->actionto check if the action is editing).
I need to add another custom post type (
player) in thatif
statement
Just change the:
'page' == $screen->id
to:
in_array( $screen->id, array( 'post', 'page', 'player' ) )
Or the full code:
if ( $screen && // 1. we've got a valid screen
'post' == $screen->base && // 2. the screen base is either post.php or post-new.php
// and 3. the post type is one of the array values.
in_array( $screen->id, array( 'post', 'page', 'player' ) )
) {
// your code here
}
I hope that helps, and you can check the get_current_screen()‘s function reference here. 🙂
