28

I use this after I checked if the user is admin

   if ( isset($_GET['action'])  && $_GET['action'] === 'edit' )

is there better way?

2
  • Through my testing today, this seems to be the best way to do this with current WP conditionals, because get_current_screen is documented as failing with a fatal error on some admin screens. See the docs for more info, codex.wordpress.org/Function_Reference/get_current_screen
    – Nathan
    Commented Mar 25, 2016 at 18:44
  • but attempting to call get_current_screen() will result in a fatal error because it is not defined. — well wrapping it in if (function_exists('get_current_screen')) then?
    – Frank N
    Commented Jan 1, 2017 at 8:11

3 Answers 3

30

You can use get_current_screen to determine this.

$screen = get_current_screen();
if ( $screen->parent_base == 'edit' ) {
    echo 'edit screen';
}

I don't know if I exactly would say this is always better, it depends on what's needed, but it's probably the way I'd do it. The big benefit with this method is that you get access to more information and ergo can do more and different distinctions. Just take a look at the documentation to understand what I mean.

It should be used in later hooks, Codex says:

The function returns null if called from the admin_init hook. It should be OK to use in a later hook such as current_screen.

5
  • This would prove true on a listing of Posts and Pages, too, though, right? Such as mywebsite.com/wp-admin/edit.php?post_status=draft&post_type=post
    – Nathan
    Commented Mar 25, 2016 at 18:42
  • 1
    @Nathan True, it does. As said in the answer, what is right for the purpose, depends on the use case. This certainly is not the best way to go for all cases, but in some cases it is a good if not even the best one. Commented Mar 26, 2016 at 13:38
  • Note: Calling get_current_screen() directly in many cases will cause a fatal error. Be sure to wrap it in a function and call it from an appropriate hook. Commented Jan 13, 2017 at 16:53
  • $screen->is_block_editor == 1 worked for me to check the edit page. parent_base appears to be empty.
    – Floris
    Commented Sep 21, 2021 at 11:25
  • parent_base is always NULL for me both in post list and in single post edit. What I used instead is if (get_current_screen()->base === 'post' && get_current_screen()->post_type === 'my_post_type') . This is for single post admin edit. For the edit.php screen (the admin screen with post list) use edit instead of post.
    – Denis P
    Commented Aug 6 at 15:27
27

Better way:

global variable $pagenow

global $pagenow;
if (( $pagenow == 'post.php' ) || (get_post_type() == 'post')) {

    // editing a page

}
if ($pagenow == 'profile.php') {

    // editing user profile page

}

source: https://wordpress.stackexchange.com/a/7281/33667

6
  • I found this to work except for $_GET['post_type']. However, get_post_type() worked for me instead. Commented Sep 14, 2017 at 16:30
  • 1
    $_GET['post_type'] is only set on post-new.php, when creating a new post, and not on post.php which is when the post is being edited. get_post_type() will work for post.php because the post already exists adn has a post type, but I'm not sure it'll work on post-new.php. Frank's answer is better as it works for both. Commented Apr 10, 2018 at 7:31
  • In the first condition, why is there an OR ( || ) rather than an AND ( && )?
    – alexg
    Commented Jun 8, 2021 at 9:25
  • than you for the help. it is best answer for some situation. Commented Nov 5, 2021 at 11:07
  • 1
    nope, how about post-new.php? there are new page names every major versions, this is unreliable.
    – scavenger
    Commented Apr 29 at 1:46
8
  • Use 'get_current_screen', just make sure beforehand, it exists.
  • As codex says "This function is defined on most admin pages, but not all."
  • This btw also filters out normal (reader-facing) views (re-read that sentence, with emphasis on admin pages).
  • quite likely the next thing you want to figure out is, if you are actually on a page or post...

    // Remove pointless post meta boxes
    function FRANK_TWEAKS_current_screen() {
        // "This function is defined on most admin pages, but not all."
        if ( function_exists('get_current_screen')) {  
    
            $pt = get_current_screen()->post_type;
            if ( $pt != 'post' && $pt != 'page') return;
    
            remove_meta_box( 'authordiv',$pt ,'normal' );        // Author Metabox
            remove_meta_box( 'commentstatusdiv',$pt ,'normal' ); // Comments Status Metabox
            remove_meta_box( 'commentsdiv',$pt ,'normal' );      // Comments Metabox
            remove_meta_box( 'postcustom',$pt ,'normal' );       // Custom Fields Metabox
            remove_meta_box( 'postexcerpt',$pt ,'normal' );      // Excerpt Metabox
            remove_meta_box( 'revisionsdiv',$pt ,'normal' );     // Revisions Metabox
            remove_meta_box( 'slugdiv',$pt ,'normal' );          // Slug Metabox
            remove_meta_box( 'trackbacksdiv',$pt ,'normal' );    // Trackback Metabox
        }
    }
    add_action( 'current_screen', 'FRANK_TWEAKS_current_screen' );
    
1
  • 1
    thanks for sharing, get_current_screen() is the right tool to use as it offeres a good amount of data.
    – Sagive
    Commented Nov 9, 2017 at 8:13

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