Feelings of White   this is where something witty goes
Me, looking like Hot Sex inc. with my cool shades, a beer and my bountiful chest hair displayed for all to see
  • all
  • curator's pick
  • funny
  • narcissism
  • technical
  • the arts
  • the void
  • violent aggression
  • writing

WordPress plugin: Apply a CSS class defined in a post’s custom field

It's not much, but it's helped the look of my site.  By applying a custom field to a specific blog post like so:

WordPress admin screenshot, showing a custom field with a key of "legioncss" and a value of "MyCssClass"

Your content (regardless of index, archive, or single placement) then renders like so:

<div class="MyCssClass">Actual Blog Post Here</div>

This allows each post to have different (but common) CSS Styles.  Other plugins allow you to apply a custom stylesheet per-post, but my plugin is more useful when there are a few common styles you want to re-used.  All that's missing is uploading the following code to your wp-content/plugins directory:

<?php

/**
 Plugin Name: Legion's Div Content Plugin
 Version: 0.1
 Description: Wraps the the_content with a custom css class specified in the "legioncss" custom field
 Author: legion
 Author URI: http://FeelingsOfWhite.com
 */

add_filter('the_content', 'legiondiv_the_content');

function legiondiv_the_content($content) {
    global $post;  //required to access $post->ID
    $customCssClass = get_post_meta($post->ID, "legioncss", true);
    if ($customCssClass != "")
        $content = '<div class="' . $customCssClass . '">' . "$content</div>";
    return $content;
}

?>

As a n00b to WordPress, it's hackability has impressed me.  I'm amazed that this is all it takes to write a plugin.  There are plenty of fantastic resources available just by searching the web.

2008 May 22 9:36 pm; Filed under technical and tagged plugin, the process, wordpress.
« The Best of Projects: Autoserious [2 of 3] « before «
» after » BSG 4×07 – Guess What’s Coming to Dinner? »
  1. Bush on 2008 Nov 05

    Fun. Add to your bookmarks. And how long it took to write articles?

  2. legion on 2008 Nov 06

    Hi Bush, good to meet you. I'm glad you liked the article enough to bookmark it :)

    The time it takes to write an article varies. This one didn't take that long, maybe an hour or two (although developing the plugin took an evening of learning). Other articles, like my recent Tamdhu stories or my .net programming article took many many hours over a period of months.

    I hope you keep coming back, Bush :)

  3. timsilverworldtravels on 2009 Aug 29

    I am stoked to meet new people here. It looks like a
    fantastic place!

    I'm into playing fishing.

    Any others here?

Posting your comment.
Follow responses with this post's comment feed.

Leave a reply

Subscribe

Recent Awesomeness

  • Watching Oprah
  • The Worst Day
  • peanut butter & bees
  • Cook it yourself
  • VA#7 Violent Aggression Overthinks Sodomy
  • Lost Untangled
  • Homunculus
  • VA#6 Violent Aggression vs. Decency
  • Bring Back The Silk Pie!
  • More to follow
  • Ken Melnichuk
  • Scary fucking shit; the pictures
  • Scary fucking shit
  • Unsold Pilot
  • VA#5 Violent Aggression vs. The Racist Candy

Find things tagged

4400 Battlestar Galactica Battlestar Galactica cliff comics curation depression erron family fiction food game janine job kelly kyle liam lost manifesto meta mlp music nathan passionate diatribes plug poem politics Really Dumb Story relationships review revisionism sam sermon software spirituality star trek Star Trek Deep Space Nine suicide tamdhu testpoint the process travels video vlad wtf

Other Opinions

  • Cliff on Watching Oprah
  • Erron on Cook it yourself
  • Erron Anderson on The Worst Day
  • Tammy on Cook it yourself
  • Legion on Cook it yourself
  • Tammy on The Worst Day
  • Cliff on The Worst Day
  • Chad on The Worst Day
  • Cliff on The Worst Day
  • Cliff on Cook it yourself
  • Tammy on peanut butter & bees
  • Chad on peanut butter & bees
  • Tammy on Cook it yourself
  • Chad on Cook it yourself
  • Stephen on Good Goddamn, Powershell Sucks!

Friends of White

  • The Ack Attack is bringing Lost & BSG awesomeness direct to your brain!
  • Peer Pressure Works to bring you football, rants, politics and kittens
  • What's Alan Watching is your television addiction
  • Jammer's Reviews provides insight into BSG and Star Trek
  • The Grind is a pixelrific exploration of WoW addiction and more
  • In The Now touched me inappropriately with its writing
  • Analog Coast amalgamates many blogs into one and lately some original content has washed ashore

What was I doing in..

  • July 2010 (4)
  • June 2010 (1)
  • May 2010 (2)
  • April 2010 (2)
  • January 2010 (1)
  • December 2009 (4)
  • November 2009 (2)
  • October 2009 (1)
  • August 2009 (2)
  • July 2009 (2)
  • June 2009 (1)
  • May 2009 (1)
  • April 2009 (3)
  • March 2009 (11)
  • February 2009 (6)
  • January 2009 (10)
  • December 2008 (3)
  • October 2008 (1)
  • August 2008 (2)
  • July 2008 (3)
  • June 2008 (1)
  • May 2008 (11)
  • April 2008 (7)
  • March 2008 (3)
  • February 2008 (1)
  • January 2008 (2)
  • December 2007 (1)
  • October 2007 (1)
  • September 2007 (3)
  • August 2007 (1)
  • June 2007 (3)
  • May 2007 (2)
  • March 2007 (5)
  • February 2007 (5)
  • January 2007 (13)
  • September 2006 (1)
  • June 2001 (3)
  • May 2001 (2)
  • April 2001 (2)
  • March 2001 (2)
  • February 2001 (1)
  • January 2001 (1)
  • November 2000 (5)
  • May 2000 (3)
  • April 2000 (5)
  • March 2000 (3)
  • February 2000 (3)
  • January 2000 (6)
  • December 1999 (17)

Copyright © 2009 Feelings of White | Powered by WordPress | Original site design by Stephen Reinhardt; tweaked by me