Those of you with PHP experience may already know by reputation how easy WordPress is to modify, and I’ve been having a lot of fun customizing themes for the past year. This is a quick and dirty post illustrating how to customize a WP theme to select a Creative Commons license for each post. After I tested this method I played around with a few WP plug-ins, including Per-Post Creative Commons License, which I liked a lot. But I still wanted to post this method for folks who don’t want to install plug-ins, or prefer using WordPress’s built-in Custom Fields functionality.
Set Up Blog Post Licenses
-
From within any blog post, add a new Custom Field called, for instance, “license”.
-
Use an easy, consistent tactic for marking your license. I’m using lowercase letters separated by dashes to describe Creative Commons licenses, e.g. cc-by-sa or cc-by-nc-nd. Use these marks in each post’s “license” field from now on.
Now you’re storing the license metadata on your blog’s server. It’s time to make it look cool in your theme.
Customize Your WP Theme
-
Isolate your theme’s folder in wp-content/themes
-
Upload whatever CC license images that you want to use to your theme’s images folder. Here’s a ZIP file of the CC images I use. Note how I named the images the same way I entered my licenses above, e.g. cc-by-sa.png or cc-by-nc-nd.png. This allows me to shortcut in the PHP.
-
Now the “hard” part: edit the files single.php and (optional) index.php. Find the spot you want to have your license display. This needs to be soon after the WP function
the_content()is called. In index.php this needs to happen before theendwhileof thehave_posts()function. -
Write (or paste in) a little PHP. In short we need to use the
get_post_meta()function to get the custom field value for this post. Here’s a quick snippet that I used to test theis approach:<?php
if($license = get_post_meta($post->ID, "license", "true")) {
?>
<a href="http://creativecommons.org/licenses/"><img src="<?php bloginfo('template_directory'); ?>/images/licenses/<?=$license?>.png" alt="<?=$license?> license" style="border: 0" /></a>
<?php
} else {
?>
© <?php the_time('Y');
}
?>
If there is nothing in the “license” custom field, it defaults to © and the post’s year.
You can do this on both single.php and index.php or anywhere else you have post information showing.
- Finally, upload and test!
