Feelings of White   In a few words, explain what this blog is about.
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
  • 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? »
Posting your comment.
Follow responses with this post's comment feed.

Leave a reply

Subscribe

Recent Posts

  • Why I’m Throwing Away My Vote
  • Comics You Must Read! #1
  • The Shit Dancer [Tamdhu Stories 1.3]
  • Money & Other Friends [Tamdhu Stories 1.2]
  • Betty the Gas-Whore [Tamdhu Stories 1.1]
  • Command Line Parsing using Attribute Decoration in .NET
  • We Are Metallica
  • The Best of Projects: Fiction [3 of 3]
  • Good goddamn, Galactica Roxxors With Coxx Out
  • BSG 4×07 - Guess What’s Coming to Dinner?
  • WordPress plugin: Apply a CSS class defined in a post’s custom field
  • The Best of Projects: Autoserious [2 of 3]
  • The Best of Projects: Eclectic [1 of 3]
  • The Four Noble Truths, phrased in a rather beautiful way
  • BSG 4×06 - Faith

Recent Comments

  • Mike on Why I’m Throwing Away My Vote
  • Cliff on Why I’m Throwing Away My Vote
  • Morpheus on Comics You Must Read! #1
  • Morpheus on The Shit Dancer [Tamdhu Stories 1.3]
  • Legion on The Shit Dancer [Tamdhu Stories 1.3]
  • Morpheus on The Shit Dancer [Tamdhu Stories 1.3]
  • Legion on The Shit Dancer [Tamdhu Stories 1.3]
  • Morpheus on The Shit Dancer [Tamdhu Stories 1.3]
  • Kyle on The Shit Dancer [Tamdhu Stories 1.3]
  • Legion on The Shit Dancer [Tamdhu Stories 1.3]

Tags

4400 bsg cliff comics curation depression ds9 erron fiction game janine job kelly kyle liam lost manifesto meta mlp music passionate diatribes plug plugin poem powershell rds relationships review revisionism sam sermon shaun software spirituality star trek suicide tamdhu testpoint the process travels video vlad wacky walkabout wtf

Archives

  • 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 © 2008 Feelings of White | Powered by WordPress | Designed by Stephen Reinhardt; tweaked by me