Wordpress - how to know if admin is in edit page or post

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.


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


  • 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' );
    

Tags:

Editor

Admin