Rewrite WP Attachment Images on the Fly

server-patch-panel

At first, this may not seem to have any real-world use, but consider this: You need to set up an independent development environment for your site, and you’d rather not copy gigabytes of files from your production server’s wp-content directory. Without the following function, keeping everything synced could turn into another full-time job, especially if your editorial staff is posting hundreds upon hundreds of articles each day.

Here’s how it works: This filter interrupts the output of wp_get_attachment_image and replaces the returned image tag’s src attribute with your production server’s url, therefore effectively telling the page to read images from a different server than the page is served from.

So here’s the WordPress code that makes it all happen. Put it in a file in your development server’s mu-plugins directory so it doesn’t get overwritten with theme synchronizations. Remember to swap out “yourserver.com” and “dev.yourserver.com” with appropriate values.

<?php

function rewrite_images( $atts ) {
	$atts['src'] = str_replace( 'dev.yourserver.com', 'yourserver.com', $atts['src'] );
	return $atts;
}
add_filter( 'wp_get_attachment_image_attributes', 'rewrite_images' );

?>

What other creative uses can you think of for this snippet? Leave me a comment below!

The Greatest Creation of my Life | Week 27

It has again been quite a while since I’ve written about our baby, mainly because up until now there hasn’t been much to say! It seems to me that it has been as normal a pregnancy as possible.

We’re getting close now! The third trimester is here and we’re starting to actually prepare for the arrival of our baby boy. Tina’s parents hosted a wonderful baby shower last weekend (view the photo gallery here) where we got to share our joy with family and friends. It was a lot of fun and we are very grateful for the amazing event that Don and Silvana put together. Thanks to all the guests and out-of-towners who sent gifts for our baby. We can’t express how thankful we are for your love and support!

So now we’re down to the final couple months before baby comes and it’s time to prepare. Here’s what we’re getting our hands dirty with lately:

  • Scheduled birthing classes for January 2013. Perfect timing for our schedules and for the pregnancy. It’s a one-day class on a Saturday that we’re attending, but seems like it will be packed with information.
  • Cleaning up the apartment and baby-proofing. This is a lot of fun, we’re having a blast. We actually started moving to Fort Lauderdale in December – we had boxes packed and papers signed – but due to the actions of incompetent real-estate agents we missed out on the deal of a lifetime. So now we’re staying in West Palm Beach for another year. So we are now unpacking our place in a slightly different arrangement.
  • Setting up baby’s room – Another fun item on our list. We’ve done a lot of reading and research and learned quite a lot. As the “man of the house”, I’m very much enjoying this task, other than the fact that I’m dismantling my office and cramming it into the living room.

We are fairly certain that we’ve picked out a name for our boy but it’s not definite until we sign the birth certificate.

I guess the next stop for this blog section will be baby room pictures. Stay tuned!

Daytona Beach in the Winter

A few random shots taken in and around Daytona Beach after the baby shower.

Hakuna Matata

Photos from Tina’s baby shower on December 8, 2012 in Daytona Beach, Florida.

2012 Photography Gallery

I wanted to put together some of my favorite pictures from 2012 in one place – so here they are! All compliments of my WordPress AutoMedia App & Plugin.

Purge All WordPress Users of a Particular Role

Bulk Delete WordPress Users

I needed to delete all of the subscriber level users on one of my sites recently, and couldn’t find an easy way to do it. My solution? Write some code. It was definitely quicker and easier to run this snippet than to manually delete over 7,700 users from my database. Drop this code into an admin page where you can run it as an administrator, and watch it go to work!

If you need to delete authors, editors, or some other role, simple switch “subscriber” on line 3 to your chosen role. Note: it must be a valid WordPress role registered in your site. This code leaves users with posts in the database. If you would like to remove users that have posts, remove the conditional on line 7 and it’s corresponding closing bracket on line 13. Be sure to modify the wp_delete_user function call on line 8 with it’s second argument – this will keep those user’s posts online and attribute them to whatever user ID you specify in the second argument.

<?php 

$all_users = get_users( array( 'role' => 'subscriber' ) );

foreach( $all_users as $single_user ) {
	$users_posts = get_posts( array( 'author' => $single_user->ID ) );
	if( !$users_posts ) {
		if( wp_delete_user( $single_user->ID ) ) {
			echo 'User ' . $single_user->ID . ' deleted.<br />';
		} else {
			echo 'Delete user ' . $single_user->ID . ' failed!<br />';
		}
	}
}

?>

Disclaimer: This code is fast and aggressive. There is no “undoing” this operation. Use this at your own risk, data is NOT recoverable. Also, don’t believe everything you read online … you cannot perform the same function with raw MySQL without completely hosing your database – so don’t even try unless you REALLY know what you’re doing.

A Day at the Palm Beach Parks

There’s no need to use lots of words here – the pictures tell the story of our day exploring MacArthur Park and Phil Foster Park.