Skip to content

Instantly share code, notes, and snippets.

@josafafilho
Created September 2, 2015 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josafafilho/d6f7716b286d588373cc to your computer and use it in GitHub Desktop.
Save josafafilho/d6f7716b286d588373cc to your computer and use it in GitHub Desktop.
Really get the current post type on Wordpress Admin
/**
* gets the current post type in the WordPress Admin
*/
public function get_current_post_type()
{
global $post, $typenow, $current_screen;
//we have a post so we can just get the post type from that
if ($post && $post->post_type)
return $post->post_type;
//check the global $typenow - set in admin.php
elseif ($typenow)
return $typenow;
//check the global $current_screen object - set in sceen.php
elseif ($current_screen && $current_screen->post_type)
return $current_screen->post_type;
//check the post_type querystring
elseif (isset($_REQUEST['post_type']))
return sanitize_key($_REQUEST['post_type']);
//check if we have the post id
elseif (isset($_REQUEST['post']))
return get_post_type($_REQUEST['post']);
//last attempt try to handle data coming from wp_autosave
$ajaxData = $_REQUEST['data']['wp_autosave'];
//check the post_type querystring
if (isset($ajaxData['post_type']))
return sanitize_key($ajaxData['post_type']);
//check if we have the post id
elseif (isset($ajaxData['post']))
return get_post_type($ajaxData['post']);
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment