Custom functions in WordPress can save time

22 04 2011

At work, I deal with lots of complex layouts and designs for websites. I also have to make sure that each site will work on two or more servers – at least our development server and the live site (typically RackSpace). This means I have to rely heavily on WordPress’ built-in functions so that links will function when the site is moved from server to server easily.

I got tired of writing the path to images in my default images folder, so I wrote my own function to act as a short cut. Where I used to write this:

<?php bloginfo('template_directory'); ?>/images/hello.jpg

I can now write this:

<?php image_dir('hello.jpg'); ?>

The function “image_dir” can take an argument that is the images file path relative to your main images directory. If you don’t provide an argument, it will just spit out the image directory  path and you can add a file name or path onto that. The image_dir() code is below.

<?php
function image_dir($loc='') {
	if ($loc == '') {
		echo get_bloginfo('template_directory').'/images';
	} else {
		echo get_bloginfo('template_directory').'/images/'.$loc;
	}
}
?>

That is pretty much it.

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: