16

I use this after I checked if the user is admin

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

is there better way?

marked as duplicate by Jack Johansson, kraftner, cjbj, Dave Romsey, cybmeta Jul 12 '17 at 11:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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 Mar 25 '16 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 Nocke Jan 1 '17 at 8:11
15

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.

  • 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 Mar 25 '16 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. – Nicolai Mar 26 '16 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. – squarecandy Jan 13 '17 at 16:53
6

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

  • I found this to work except for $_GET['post_type']. However, get_post_type() worked for me instead. – Ashe Abbott Sep 14 '17 at 16:30
  • $_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. – Jacob Peattie Apr 10 '18 at 7:31
5
  • 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
    thanks for sharing, get_current_screen() is the right tool to use as it offeres a good amount of data. – Sagive SEO Nov 9 '17 at 8:13
0

To check if the current use is a admin I like to use the following function:

<?php    
function is_user_admin(){
    if ( is_user_logged_in() ){
        if( current_user_can( 'manage_options' ) )
        {
          return true;
        }
        else
        {
          return false;
        }
    }
    return false;
    }
?>
  • I'm aware this is an old answer, but in case someone comes across this, I thought it should be pointed out that this is wrong. is_admin() only checks if the WordPress administration panel is trying to show. Any user that gets the bar at the top of their screen would pass this test. – Dylan Hildenbrand May 6 '15 at 1:43
  • 1
    Updated my answer to remove the incorrect portion. – Vigs May 6 '15 at 2:00

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