Skip to content

Tag Archive for PHP

Programmatically Pull Attachments from WordPress Posts

One of my favorite tricks in using WordPress as a CMS is to have it handle photo resizing and insertion into pages automatically. For example, swissmiss and I just launched Convert (a NYC based Green Roof Service) today. Within the projects section they display photos of the green roofing projects they’ve done and let you download a project sheet PDF.

It would be quite a burden for the client to have to know how to create all of the appropriate image sizes, upload the photos and insert the correct HTML tags. So here’s what we’ve done instead:

First, we set the WordPress image sizes to match the thumbnail and large sizes that we needed by going to Settings->Media in the admin tool.

Next, the client just has to upload all of their photos and a PDF for the project sheet. The images are automatically scaled by WordPress and attached to the page. They can also change the order the photos appear by dragging/dropping them in the admin tool.

Finally, adding this simple snippet of code to the functions.php in their theme, I can ask for all of the images associated with the post.

// get all of the images attached to the current post
function aldenta_get_images($size = 'thumbnail') {
	global $post;

	$photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

	$results = array();

	if ($photos) {
		foreach ($photos as $photo) {
			// get the correct image html for the selected size
			$results[] = wp_get_attachment_image($photo->ID, $size);
		}
	}

	return $results;
}

So, if you want all of the thumbnail images (as an array of html tags) you can make the default call:

$photos = aldenta_get_images();

If you want all of the medium sized images you pass medium as the size:

$photos = aldenta_get_images('medium');

My function has a bit more going on inside of it but you get the idea of what you can do with this. Another way I like to use it is to pull the first image from the post. Sometimes, you want to display a thumbnail image that represents a post. Here’s how I like to do that:

// get the first image attached to the current post
function aldenta_get_post_image($size = 'thumbnail') {
	global $post;

	$photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

	if ($photos) {
		$photo = array_shift($photos);
		return wp_get_attachment_image($photo->ID, $size);
	}

	return false;
}

// the html tag for the first image or false if no image is found
$photo = aldenta_get_post_image();

What if you want to get the PDF attached to the post? No problem – it works the same but you change the mime type to application/pdf.

// get the first PDF attached to the current post
function aldenta_get_post_pdf() {
	global $post;

	$attachments = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'application/pdf', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

	if ($attachments) {
		$attachment = array_shift($attachments);
		return wp_get_attachment_url($attachment->ID);
	}

	return false;
}

// the url to the first pdf or false if no pdf is found
$pdf = aldenta_get_post_pdf();

This technique has helped me tremendously and I hope it does the same for you.

WordPress 2.5 is officially out

WordPress 2.5

It’s official. WordPress 2.5 is now out with a crisp new admin interface and great new features. They’ve even redone the website. Here are some of my favorite changes:

  • A beautiful new admin interface.
  • A much more intuitive flow when making a post.
  • Multiple instant file uploads.
  • Thumbnail, medium and full-sized image options (and you can change the image dimensions in Settings).
  • A quick way to display your photos as a gallery
  • Easily add/edit tags so no more plugins are needed.
  • A visual editor that doesn’t break your code (this one is great for my clients).
  • Search now looks at posts and pages.

I’ve always thought that WordPress is one of the best blogging and content management systems. It’s incredibly intuitive for the user (even more so now) and extremely flexible for developers.

Thanks so much to everyone who has helped with WordPress!

Getting Apache & PHP to work with Leopard (OS X 10.5)

I ran into a snag today while trying to get the websites on my local machine running with Leopard. The first thing was that the config file in the new version of Apache is in a different location (/etc/apache2/httpd.conf instead of /etc/httpd/httpd.conf). Once I changed the settings I kept getting 403 Forbidden messages every time I loaded a page (the default in the new httpd.conf is ‘Deny from all’ for all directories). I finally figured things out and here’s what I did:

  1. Open the httpd.conf file for editing (sudo vi /etc/apache2/httpd.conf).
  2. Uncomment the line
    LoadModule php5_module     libexec/apache2/libphp5.so
    by removing the ‘#’ from the front of the line. This will enable PHP 5. The php5.conf file is loaded automatically from /private/etc/apache2/other/.
  3. Create a file called _sites.conf in your Sites folder. I like to keep my site configuration in the Sites folder to make it more easily accessible as I’m doing development.
  4. Add the following line to the very bottom of the httpd.conf file:
    Include /Users/yourusername/Sites/_sites.conf
    That will make Apache load all the configuration settings from your _sites.conf file.
  5. Add the following information to your _sites.conf file.
# Enable named virtual hosts
NameVirtualHost *:80

# Override the default httpd.conf directives.  Make sure to
# use 'Allow from all' to prevent 403 Forbidden message.
<Directory />
	Options ExecCGI FollowSymLinks
	AllowOverride all
	Allow from all
</Directory>

# A basic virtual host config
<VirtualHost *:80>
	# Add yoursite to your /etc/hosts file so you can
	# type it directly in your browser
	ServerName yoursite

	DocumentRoot /Users/yourusername/Sites/yoursite
</VirtualHost>

Now you just need to start/restart Apache and the sites should load. PHP should be working and no more 403 Forbidden message.

Moment of Open Source Zen

Every day, as a developer, I’m grateful for the thousands of programmers around the world who have so graciously shared their time, knowledge, and programming code. I have access to millions of lines of free code at any moment through the Internet which allows me to write programs more easily, learn new concepts, and make a difference in society.

Yes, every open source developer plays their part in making life better. Take, for example, the recent updates to Sean Coon’s blog. A few tweaks to some great free programming code and Sean’s rapidly spreading the word about The People, Yes which directly engages the homeless community to share their voice through blogging. He’s able to send a text message which is then automatically relayed to his friends/acquaintances (through Twitter) and also posted to his blog (using a modified version of Alex King’s Twitter Tools plugin). Don’t forget that in conjunction with this great plugin there are other pieces of the free code puzzle – the powerful blogging tool, the simple programming language, and the server’s solid operating system. All of this was made possible by many generous programmers sharing their time and skills.

So here’s to the programming language, plugin, framework, and code snippet givers around the globe. You truly make a difference in the world.