1

I have few lines of code in a theme, I did not understood, can anyone explain me this.

$screen = get_current_screen(); // understood this
if ( $screen && 'post' == $screen->base && 'page' == $screen->id ) { // not sure about this line
// code to execute something
}

As per I have understood the code is executed when post type is post or page, I am not sure how, I need to add another custom post type (player) in that if statement.

closed as off-topic by Michael, fuxia Aug 7 at 10:01

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Actually, that conditional means, check if the base of the current file in the URL is post and that the post type is page. I.e. If you're editing a Page, the conditional would return true. If you want to check for another post type, just change the 'page' == $screen->id to in_array( $screen->id, [ 'page', 'player', 'etc-cpt' ] ) – Sally CJ Aug 2 at 2:35
  • 1
    @SallyCJ That sounds like a wonder answer you could post in the answer section below :) – Howdy_McGee Aug 2 at 14:10
1

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->id is equivalent to $screen->post_type which is the post type (slug).

  • And $screen->base is post which 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 is post.php, the base name is post. However, when creating a post/Page/CPT, the file name is actually post-new.php, but still the $screen->base is post (and not post-new) because WordPress strips the -new part.

  • Additionally, you can distinguish between editing and creating by checking the screen's action; i.e. whether $screen->action is edit or add (e.g. 'edit' == $screen->action to check if the action is editing).

enter image description here

I need to add another custom post type (player) in that if 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. :)

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