WordPress plugin: Query Tester

@fienen came up with a good idea for a WordPress plugin today. He wants to be able to type in a sample parameter string for query_posts and see which posts will result, given that string. I decided that’s something I want too so I don’t have to hack up a template on the fly if I don’t want to.

Here’s a quick and dirty plugin which should do the trick. It installs itself into Dashboard > Settings > Query Tester

Prettier PHP Debug Messages

(prettier than what I’ve been doing, at least)

Hmm, let’s see. Trying to figure out a bug in my WordPress template. I need to know what the current post looks like. So I’ll go to the template and put in


which produces

That’s helpful, but a little hard to read. So let’s create a Pretty Print function and put some preformat tags around the output.

(in my theme’s functions.php)

if (! function_exists('pp')) {
  function pp($msg) {
  	echo "<" . "pre>" . print_r($msg,true) . "<" . "/pre>";

And so now if I call this in my template


we get

A little better. So that’s been my Pretty Print function for quite a while. But sometimes the debug messages are unreadable due to their position in the template where I call pp(). Maybe there are theme colors hiding the messages, or possibly an overflow: hidden CSS rule blocks some of them.

So I decided to make another version which will collect all of the calls to pp() and put them in one visible spot. It will allow scrolling, and CSS styling.

(in my theme’s functions.php)

if (!function_exists('pp')) {
  function pp($obj) {  
    $data = json_encode(print_r($obj,true));
    <script type="text/javascript">
      var obj = <?php echo $data; ?>;
      var logger = document.getElementById('bsdLogger');
      if (!logger) {
        logger = document.createElement('div');
        logger.id = 'bsdLogger';
      var pre = document.createElement('pre');
      pre.innerHTML = obj;

So now in my template, I debug all sorts of nonsense

            <?php the_content(); ?>
            <?php pp(Array('apples','oranges'));
            <table style="border: 2px solid red;">
              <tr><td>What's this?</td><td>a table in the middle of nowhere?</td></tr>

Mix with some CSS to taste and you get

Notice that even though there’s a random table happening between debug calls, the debug messages still gather into their designated spot.

I think I’ll see how that suits me for a while. True, it obscures part of the content, but if I’m asking for a debug message in the first place, that’s primarily what I want to see. When I remove the pp() calls in the code, the message box goes away again.

If there are better ways of doing this, please let me know.

Relative Site URL on Content Save

Another post for WordPress admins, building on an earlier WordPress Tip: Bloginfo as a Shortcode

Having a shortcode is great, but why not automate the insertion of the shortcode into the content box? That’s what I’ve done here.

(To use this code, add it to your theme’s functions.php file)

function bsdrelative_content_replace_url($content) {  
  return str_replace(get_bloginfo('url'),'[url]',$content);
function bsdrelative_url_shortcode($atts, $content = null) {
  return get_bloginfo('url'); 
add_shortcode("url", "bsdrelative_url_shortcode");

There’s a reason I’m using a shortcode rather just replacing http://site.com/the/link with /the/link and it’s that the latter assumes that the site is installed at the DocumentRoot of the webserver. Not a safe assumption. Often times, I’m developing a new website for a client under a subfolder such as a http://clientsite.com/staging or http://clientsite.com/dev while the current to-be-replaced site remains at the root level.

I’ve also made this available as a WordPress Plugin