Advertisement
daymobrew

WordPress Debug Output

Aug 6th, 2014
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.27 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Debug Information
  4. Plugin URI: http://www.damiencarbery.com
  5. Description: Add debug information as a html comment for the specified IP address.
  6. Author: Damien Carbery
  7. Version: 0.1
  8.  
  9. $Id: mu-debug-info.php 2407 2014-07-31 11:32:16Z damien $
  10. */
  11.  
  12. add_action( 'wp_head', 'mu_debug_info' );
  13. //add_action( 'loop_start', 'mu_debug_info' ); // Use if have issues getting page or post ID.
  14. add_action( 'wp_footer', 'mu_debug_info_footer', 40 );
  15.  
  16. function mu_debug_info() {
  17.   if (current_user_can('edit_theme_options')) {
  18.     echo '<!-- Server Debug Info from mu-debug-info plugin.', "\n\n";
  19.  
  20.     // WordPress template tags.
  21.     echo 'WordPress conditional tags information.', "\n";
  22.     $conditional_tags = array(  'is_home', 'is_front_page', 'is_page', 'is_page_template', 'is_single',
  23.                         'is_singular', 'is_category', 'is_archive', 'is_author', 'is_search', 'is_404',
  24.                         'is_paged', 'is_attachment', 'is_feed', 'in_the_loop', 'is_multi_author',
  25.                         'is_date', 'is_year', 'is_month', 'is_day', 'is_time',
  26.                         'is_tag', 'is_tax', 'is_sticky',
  27.                         'is_comments_popup', 'comments_open', 'pings_open',
  28.                         'is_admin');
  29.     foreach ($conditional_tags as $cond_tag) {
  30.       echo " $cond_tag(): ", call_user_func($cond_tag) ? 'true' : 'false', "\n";
  31.     }
  32.     // Template tags that do not return true/false.
  33.     $other_tags = array('get_post_type');
  34.     foreach ($other_tags as $cond_tag) {
  35.       echo " $cond_tag(): ", call_user_func($cond_tag), "\n";
  36.     }
  37.  
  38.     echo "\n";
  39. //    echo '  is_singular(): ', is_singular() ? 'true' : 'false', "\n"; // True when any of is_single(), is_page() or is_attachment() is true.
  40. //    echo '  is_multi_author(): ', is_multi_author() ? 'true' : 'false', "\n"; // Version 3.2+ only.
  41.     echo 'Information about the current page/post.', "\n";
  42.    
  43.     // Info about the current page.
  44.     global $wp_query;
  45.     global $post;
  46.     $page_post_ID = $post->ID;
  47.     echo ' ID: ', $post->ID, "\n";
  48.     echo ' Page ID: ', is_page() ? $wp_query->get_queried_object_id() : 'N/A', "\n";
  49.     echo ' Post ID: ', is_single() ? $wp_query->get_queried_object_id() : 'N/A', "\n";  // Maybe $post = get_page($page_id);
  50.     echo "get_post(): "; var_dump(get_post($page_post_ID, ARRAY_A));
  51.     if (is_single()) { echo "Custom Fields: "; var_dump(get_post_custom($page_post_ID)); }
  52.    
  53.     echo "\n";
  54.  
  55.     echo "Current theme supports?\n";
  56.     foreach (array('custom-header', 'custom-background', 'post-thumbnails', 'menus', 'automatic-feed-links', 'editor-style', 'widgets') as $feature) {
  57.       echo '   ', $feature, ': ', current_theme_supports($feature) ? 'true' : 'false', "\n";
  58.     }
  59.    
  60.     echo "\n";
  61.  
  62. // TODO: Display Author info on is_single() posts. May need to be done in the loop - maybe a hook to store the data for display in footer.
  63.  
  64.     // WordPress settings.
  65.     echo "WordPress get_bloginfo() information.\n";
  66.     $bloginfo_options = array( 'name', 'description', 'wpurl', 'url', 'admin_email', 'charset', 'version',
  67.                                'html_type', 'text_direction', 'language', 'stylesheet_url', 'stylesheet_directory',
  68.                                'template_url', 'template_directory', 'pingback_url', 'atom_url', 'rdf_url',
  69.                                'rss_url', 'rss2_url', 'comments_atom_url', 'comments_rss2_url');
  70.     foreach ( $bloginfo_options as $show ) {
  71.       echo "  $show: ", get_bloginfo($show), "\n";
  72.     }
  73.     echo "\n";
  74.    
  75.     // PHP global variables.
  76.     echo 'Current PHP version: ' . phpversion();
  77.     echo "\n\$_SERVER: "; var_dump($_SERVER);
  78.     echo "\n\$_GET: "; var_dump($_GET);
  79.     echo "\n\$_POST: "; var_dump($_POST);
  80.     // Sometimes $_SESSION is not set so check to avoid a PHP warning.
  81.     echo "\n\$_SESSION: "; if (isset($_SESSION)) { var_dump($_SESSION); } else { echo 'Not set.'; }
  82.     echo "\n";
  83.  
  84.     // Get template filename.
  85.     echo "\nget_template_name: ", get_template_name(), "\n";
  86.     echo "\nget_current_template: ", get_current_template(), "\n";
  87.     echo "\nshow_template: ", show_template(), "\n";
  88.  
  89.     //print_r( debug_backtrace() );
  90.  
  91.     echo ' -->', "\n";
  92.   }
  93. }
  94.  
  95. function mu_debug_info_footer() {
  96.   if (current_user_can('edit_theme_options')) {
  97.     echo '<!-- Server Debug Info from mu-debug-info plugin.', "\n\n";
  98.     echo get_num_queries (), ' SQL queries done.', "\n";
  99.     echo 'Page generation took ', timer_stop(), ' seconds.', "\n";
  100.     echo ' -->', "\n";
  101.   }
  102. }
  103.  
  104. // http://wordpress.org/support/topic/get-name-of-page-template-on-a-page#post-1267435
  105. // this can live in /themes/mytheme/functions.php, or maybe as a dev plugin?
  106. function get_template_name () {
  107.     foreach ( debug_backtrace() as $called_file ) {
  108.         foreach ( $called_file as $index ) {
  109.             if (isset($index[0])) {  // Check that this element is set to avoid PHP warning.
  110.                 if ( !is_array($index[0]) AND strstr($index[0],'/themes/') AND !strstr($index[0],'footer.php') ) {
  111.                     $template_file = $index[0] ;
  112.                 }
  113.             }
  114.         }
  115.     }
  116.     $template_contents = file_get_contents($template_file) ;
  117.     preg_match_all("(Template Name:(.*)\n)siU",$template_contents,$template_name);
  118.     if (isset($template_name[1][0])) {  // Check that this element is set to avoid PHP warning.
  119.       $template_name = trim($template_name[1][0]);
  120.     }
  121.     else {
  122.       $template_name = '';
  123.     }
  124.     if ( !$template_name ) { $template_name = '(default)' ; }
  125.     $exploded_template_name = explode('/themes/', basename($template_file));
  126.     $template_file = array_pop($exploded_template_name);
  127.     return $template_file . ' > '. $template_name ;
  128. }
  129.  
  130. // From: http://wordpress.stackexchange.com/questions/10537/get-name-of-the-current-template-file
  131. add_filter( 'template_include', 'var_template_include', 1000 );
  132. function var_template_include( $t ){
  133.     $GLOBALS['current_theme_template'] = basename($t);
  134.     return $t;
  135. }
  136.  
  137. function get_current_template( $echo = false ) {
  138.     if( !isset( $GLOBALS['current_theme_template'] ) )
  139.         return false;
  140.     if( $echo )
  141.         echo $GLOBALS['current_theme_template'];
  142.     else
  143.         return $GLOBALS['current_theme_template'];
  144. }
  145.  
  146. //add_action('wp_head', 'show_template');
  147. function show_template() {
  148.     global $template;
  149.     return basename($template);
  150. }
  151. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement