Skip to content
Jonathan Cutrell edited this page Aug 16, 2013 · 22 revisions

Here, you will find a list of useful snippets/tools you might easily forget. Editor's note: This is not the place to put passwords!

Wordpress Debug trace

This puts a trace of all of the called files in a comment. Place in the header.php of the theme files.

 if ( current_user_can( 'administrator' ) ) {   
  echo "<!--";   
  print_r( debug_backtrace() );   
  echo "-->";   
}   

Wordpress find and replace all instances of old url

update wp_posts set post_content = replace(post_content, 'http://olddomain.com','http://newdomain.com');
update wp_posts set guid = replace(guid, 'http://olddomain.com','http://newdomain.com');
update wp_options set option_value = replace(option_value, 'http://olddomain.com','http://newdomain.com');

Wordpress Multiple Post Thumbnails: Get Just the Image URL

This is useful for getting the url for background-image inline.

$img = MultiPostThumbnails::get_post_thumbnail_id('post', 'secondary-image', $post->ID);   
$imgArray=wp_get_attachment_image_src($custom,'secondary-image-size');   
$url = $imgArray[0];   

Wordpress Loop Trick: Add Class Based On Iteration Count

<?php   
$i=0;   
$extraclass="";   
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();   
if ($i==0){   
$extraclass.=" first";   
}   
?>   
<div class="<?php echo $extraclass; ?>"></div>   
<?php endwhile; $i++; endif; ?>   

Or, if you need to add a class to every third element, try this:

<?php   
$i=1;   
$extraclass="";   
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();   
if ($i%3==0 && $i>1){   
$extraclass.=" third";   
}   
?>   
<div class="<?php echo $extraclass; ?>"></div>   
<?php endwhile; $i++; endif; ?>   

Notice the difference; the $i variable starts at 1 instead of 0 here to make iteration counting easier, and we use the modulus operator to check to make sure the element is the third.

Another common problem is inserting .row divs every x elements. Here is a pattern to use to solve that problem.

<?php   
$i=1;   
$extraclass="";   
if ($query->have_posts()) : echo "<div class='row'>"; while ($query->have_posts()) : $query->the_post();   
?>   
<?php if (($i-1)%3==0 && $i != 0){ echo "<div class='row'>"; } ?>
<div class='article'></div>   
<?php if ($i%3==0 && $i != 0){ echo "</div><!-- ending .row -->"; } ?>
<?php endwhile; $i++; endif; if ($i%3!=0){ echo "</div>"; } ?>   

Here, we are using a roundabout way of checking our iteration numbers. Specifically, we wrap the whole call in a <div class="row"> that closes out if the final element doesn't complete our proverbial three-wide grid (which would trigger the row completion on its own). The code really explains itself.

Center a fluid image vertically

The trick here is to put the image inside a display:table element, and set it to display:table-cell.

section.photo.full {
  display: table;
  width: 100%;
  height: 100%;
  position: absolute;
  overflow: hidden;
  text-align: center;
}

section.photo.full a {
  outline: 0;
  display: table-cell;
  vertical-align: middle;
}

section.photo.full img {
  margin-top: 0;
}

Avoid Downloading Wordpress Uploads Directory with a RedirectMatch

Pesky missing images annoying you on a Wordpress local install? Don't download them to your precious, tiny hard drive. Use this directive instead.

RedirectMatch example/wp-content/uploads/(.*) http://example.org/wp-content/uploads/$1

This assumes that you're running your site at localhost/example.

Find the director for a MySQL database flat files

Run this SQL query:

SELECT @@datadir, @@innodb_data_home_dir

You can run this through the MySQL command line or PHPMyAdmin

###Loops in LESS

@iterations : 10;
.loopingClass (@index) when (@index > 0) {
	&:nth-child(@{index}) a {
		transition-delay: (0s + @index/10) + 0.1s;
		transition-duration: 0.8s + ((@iterations - @index)*.05s);
	}
	.loopingClass(@index - 1);
}
.loopingClass (0) {}
.loopingClass(@iterations);

Clone this wiki locally