I use this after I checked if the user is admin
if ( isset($_GET['action']) && $_GET['action'] === 'edit' )
is there better way?
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 theadmin_init
hook. It should be OK to use in a later hook such ascurrent_screen
.
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
.
global variable $pagenow
global $pagenow;
if (( $pagenow == 'post.php' ) || (get_post_type() == 'post')) {
// editing a page
}
if ($pagenow == 'profile.php') {
// editing user profile page
}
$_GET['post_type']
. However, get_post_type()
worked for me instead.
Commented
Sep 14, 2017 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.
Commented
Apr 10, 2018 at 7:31
||
) rather than an AND ( &&
)?
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' );
but attempting to call get_current_screen() will result in a fatal error because it is not defined.
— well wrapping it inif (function_exists('get_current_screen'))
then?