Feelings of White   i wish i had raped the monkey but what i did instead was good too
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? »
  • http://filmnew.ru Bush

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

  • http://feelingsofwhite.com legion

    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 :)

  • http://www.ted.com/profiles/view/id/288752 timsilverworldtravels

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

    I'm into playing fishing.

    Any others here?

blog comments powered by Disqus

Subscribe

Recent Awesomeness

  • Neurotic Dad
  • #8 VA vs. Brevity
  • Lost in the Epilogue
  • Jamie Oliver’s Food Revolution
  • I shoulda got a fake fingernail
  • How did this shit get started?
  • Feeding the dragon, or plant, or.. shit I’m out of time
  • Why are you eating so much soy and corn?
  • Cop Out
  • I’m A Stay At Home Dad!
  • Firefly & Serenity
  • The Kitchen of Zarro Boogs
  • America’s Got Talent FTW!
  • Steamed Salmon with Tomato Basil Couscous
  • Dream Stealers Like Me

Other Opinions

Sorry, I know colors suck, I will fix it sometime...

Find things tagged

4400 Battlestar Galactica Battlestar Galactica cliff comics curation depression erron family fiction food funny game janine job kelly kyle liam lost manifesto meta mlp music nathan passionate diatribes plug poem 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

What was I doing in..

  • March 2011 (2)
  • August 2010 (23)
  • 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