-
-
Save deckerweb/61bb94cae4c2a69f142bb3e087e2eadc to your computer and use it in GitHub Desktop.
Get the current post_type context in the WordPress admin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* gets the current post type in the WordPress Admin | |
*/ | |
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'] ); | |
} | |
//lastly check if post ID is in query string | |
elseif ( isset( $_REQUEST['post'] ) ) { | |
return get_post_type( $_REQUEST['post'] ); | |
} | |
//we do not know the post type! | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment