Front-end WordPress debugging on a live website

Okay, everybody knows it's not wise to use thing like print_r(), or echo a variable, on the front-end of a live website. It just doesn't like very professional of your visitors see these things.
However, sometimes you just need to check of fix something fast, so what can we do?

A WordPress friendly print_r() function

What we need is a WordPress friendly function that can print our variable like print_r does, but only visible to us! To keep it simple I've named it print_ar().
In the most simple version it looks like this:

1 2 3 4 56 
function print_ar( $Data ){         if ( is_user_logged_in() && current_user_can('delete_pages') ) {         print_r( $Data);                  }}

The if statement if ( is_user_logged_in() && current_user_can('delete_pages') ) checks if the current user is an admin, and if so it will print the value of the variable.

This is a nice start, but we can improve. A simple addition would be to wrap the printed value in <pre> tags so a printed array looks nice and readable.
Or give our result a CSS class so we can style it to our liking. And maybe we can notify what kind of variable is being printed. For example when print_r() prints out an 13, you don't know if it's an integer or a string. And that could make a big difference in the rest of your code.

If we do all this we get something like this:

1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 
function print_ar( $Data ){         if ( is_user_logged_in() && current_user_can('delete_pages') ) {                 echo'<div class="ar_var_dump">';                if( is_object( $Data ) ){             echo'<b>OBJECT RESULT:</b><br/>';               echo'<pre>';             print_r( $Data );            echo'</pre>';         }else if( is_array( $Data ) ){             echo'<b>ARRAY RESULT:</b><br/>';               echo'<pre>';             print_r( $Data);            echo'</pre>';         }else if( is_int( $Data ) ){             echo'<b>NUMBER RESULT: '.$Data.'</b><br/>';         }else if( is_string( $Data ) ){             echo'<b>STRING RESULT: '.$Data.'</b><br/>';        }else{             echo'<b>RESULT: '.$Data.'</b><br/>';         }                 echo'</div>';     } }

The result is now wrapped in a div with a class so we can set our CSS to our liking. Then in the following if/else statement we check what kind of data we are printing, and eventually print it when we found the type.

Print the name of the variable

I was very happy with my result so far, only one thing I would have liked; to print the name of the variable.
But that went above my coding skills..., but not above my Google skills! I've found an answer on stackoverflow.com, and with some modifications my final WordPress friendly variable dump is this:

1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 
function print_ar( $Data ){         if ( is_user_logged_in() && current_user_can('delete_pages') ) {                 echo'<div class="ar_var_dump">';                if( ini_get('allow_url_fopen') ) {             $backtrace= debug_backtrace();             $backtrace= $backtrace[0];             $fh= fopen( $backtrace['file'], 'r' );              $line= 0;             while ( ++$line <= $backtrace['line'] ) $code= fgets( $fh );             fclose( $fh );             preg_match( '/' . __FUNCTION__ . 's*((.*))s*;/u', $code, $name );            $varName= trim( $name[1] );         }else $varName= 'VARIABLE';                 if( is_object( $Data ) ){             echo'<b>OBJECT RESULT OF '.$varName.':</b><br/>';               echo'<pre>';             print_r( $Data );             echo'</pre>';         }else if( is_array( $Data ) ){             echo'<b>ARRAY RESULT OF '.$varName.':</b><br/>';               echo'<pre>';             print_r( $Data);             echo'</pre>';         }else if( is_int( $Data ) ){             echo'<b>NUMBER RESULT: '.$varName.' = '.$Data.'</b><br/>';        }else if( is_string( $Data ) ){             echo'<b>STRING RESULT: '.$varName.' = '.$Data.'</b><br/>';         }else{             echo'<b>RESULT: '.$varName.' = '.$Data.'</b><br/>';         }                echo'</div>';      } }

I'm using this for a while now, and for me it works perfect! And maybe I'm using it right now on this page without you seeing it! 😉

To conclude, lets see what it will look like. Lets print an array and a string, something like this:

1 2 3 4 5
$testArray= array( 'this', 'is', 'an', 'array'); $testString= 'This is a string';             print_ar( $testArray ); print_ar( $testString );

This will result in this:

wordpress friendly front end debug print_r var dump

print_ar result of an array and a string

 

 

  • I apologize for my poor English. I should have paid more attention in school...
    If there are any huge mistakes please inform me
  • I've never studied C, Javascript or PHP. Everything I know is learned via youtube and google.
    I realize my code is often unnecessarily long, but I prefer writing it this way so I can still understand myself later on. If there are other mistakes please let me know.