Using WP Custom Fields to Add CC Licensing to Blog Posts

Jan 7, 2009 at 8:00 am, Jared Stein

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

  1. From within any blog post, add a new Custom Field called, for instance, “license”.

  2. 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

  1. Isolate your theme’s folder in wp-content/themes

  2. 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.

  3. 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 the endwhile of the have_posts() function.

  4. 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 {
       ?>
       &copy; <?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.

  5. Finally, upload and test!

4 Responses to “Using WP Custom Fields to Add CC Licensing to Blog Posts”

  1. Elena Says:

    Excellent! I’m using a plugin, but why overloading the mysql table and monthly traffic with plugins if wordpress itself can do the job?
    Thanks!

  2. Mr. Jared Stein Says:

    Thanks Elena; glad it was useful. I almost didn’t post this because I worried nobody would bother with it.

  3. Web Developer - Shom Says:

    Thank you for important and useful code. I have tried this and it runs successfully. It is also very easy to use. Thanks again for sharing this.

  4. Pamela Scoot Says:

    Useful post for those have a little knowledge about Word Press. Thanks for your PHP code for the beginners of PHP. I have some experience using WP . So please bring other excellent features of WP to your visitors.