Skip to content

Instantly share code, notes, and snippets.

@JayWood
Created November 23, 2016 18:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayWood/11b47cec184e16ea7d7e8de05b1421cd to your computer and use it in GitHub Desktop.
Save JayWood/11b47cec184e16ea7d7e8de05b1421cd to your computer and use it in GitHub Desktop.
Finds registered post types from their originating location using debug_backtrace()
function my_registered_type( $post_type ) {
if ( 'shop_order' !== $post_type ) {
return;
}
$backtrace = debug_backtrace();
$iterator = count( $backtrace ) - 1;
if ( $iterator <= 0 ) {
return;
}
$plugin_dir = WP_PLUGIN_DIR;
$theme_dir = get_stylesheet_directory();
$parent_dir = get_template_directory();
// Okay we have the key, now walk backwards
while ( $iterator > 0 ) {
$cur_key = $backtrace[ $iterator ];
if ( isset( $cur_key['file'] ) ) {
if ( 0 === strpos( $cur_key['file'], $plugin_dir ) ){
// This is a plugin
error_log( __FUNCTION__ . '::' . __LINE__ );
return;
} elseif ( 0 === strpos( $cur_key['file'], $theme_dir ) ) {
// This is a theme issue
error_log( __FUNCTION__ . '::' . __LINE__ );
return;
} elseif ( 0 === strpos( $cur_key['file'], $parent_dir ) ) {
// This is a parent theme issue
error_log( __FUNCTION__ . '::' . __LINE__ );
return;
}
}
$iterator--;
}
}
add_action( 'registered_post_type', 'my_registered_type' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment