<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Digging into WordPress &#187; Design</title>
	<atom:link href="http://digwp.com/tag/design/feed/" rel="self" type="application/rss+xml" />
	<link>http://digwp.com</link>
	<description>Take your WordPress skills to the next level.</description>
	<lastBuildDate>Fri, 18 May 2012 18:21:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Custom CSS Per Post</title>
		<link>http://digwp.com/2010/02/custom-css-per-post/</link>
		<comments>http://digwp.com/2010/02/custom-css-per-post/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 20:17:20 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[header]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1221</guid>
		<description><![CDATA[I&#8217;ve long been a fan of &#8220;art directing&#8221; posts. That is, to apply unique CSS styling to an individual page of content when the situation calls for it. In the past, I&#8217;ve used the Art Direction plugin and I even created a screencast on using it. As it turns out, there is a major problem [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve long been a fan of &#8220;art directing&#8221; posts. That is, to apply unique CSS styling to an individual page of content when the situation calls for it. In the past, I&#8217;ve used the <a href="http://wordpress.org/extend/plugins/art-direction/">Art Direction plugin</a> and I even <a href="http://css-tricks.com/video-screencasts/77-styling-an-individual-article/">created a screencast</a> on using it. </p>
<p>As it turns out, there is a <strong>major problem</strong> with the art direction plugin. Using it with <em>any caching plugin</em> will result in a crazy epic meltdown of your site. Without too much gory detail, in trying to cache my blog <a href="http://css-tricks.com">CSS-Tricks</a>, I tried all the major caching plugins (DB Cache, WP Super Cache, W3 Total Cache) and ultimately it would trash my WordPress database and serve up white pages. Very not good. The happy ending is that Frederick Townes from <a href="http://www.w3-edge.com/">W3 Edge</a> and creator of the W3 Total Cache plugin helped me out by patching the art direction plugin and getting CSS-Tricks cached with <a href="http://wordpress.org/extend/plugins/w3-total-cache/">W3 Total Cache</a>. I would love to release the updated code, but it&#8217;s not my code to release. We managed to get in touch with the <a href="http://noel.io/">original author</a>, who said he planed to eventually update it but didn&#8217;t sound too particularly interested in the patch.</p>
<p>SO, with that extensive backstory, what is a poor fellow to do if they want to apply custom CSS to pages TODAY? Couple ideas, read on.</p>
<p><span id="more-1221"></span></p>
<h3>style-XXXX.css</h3>
<p>One of my <a href="http://digwp.com/2010/01/wordpress-wishes/">&#8220;WordPress Wishes&#8221;</a> was that you could drop an appropriately named CSS file into a theme and it would recognize it and apply itself to the proper post. For example, /art-direction/style-XXXX.css, where XXXX is the ID of the post.</p>
<p>Reader <a href="http://tween.ir/blog/">Hassan</a> <a href="http://digwp.com/2010/01/wordpress-wishes/#comment-3072">commented</a> with a cool idea for the functions.php file:</p>
<pre><code>function artStyle() {
    global $post;
    if (is_single()) {
        $currentID = $post-&gt;ID;
        $serverfilepath = TEMPLATEPATH.'/art-direction/style-'.$currentID.'.css';
        $publicfilepath = get_bloginfo('template_url');
        $publicfilepath .= '/art-direction/style-'.$currentID.'.css';
        if (file_exists($serverfilepath)) {
            echo "&lt;link rel='stylesheet' type='text/css' href='$publicfilepath' media='screen' /&gt;"."\n";
        }
    }
}
add_action('wp_head', 'artStyle');</code></pre>
<p>I tested it out and it works great. To use it, simply create a new folder called &#8220;art-direction&#8221; in your theme. Then to style any particular Post or Page, just drop a file in that folder named style-XXXX.css where XXXX is the ID of the Post or Page. When that Post or Page loads, WordPress will look for a file of that name. If it exists, it will load in in the head section. </p>
<h3>Custom Panel</h3>
<p>Reader <a href="http://www.kerricklong.com/">Kerrick Long</a> <a href="http://digwp.com/2010/01/wordpress-wishes/#comment-3035">commented</a> another cool solution. Similar to the Art Direction plugin, this adds an input area below the main content writing area. For the functions.php file:</p>
<pre><code>//Custom CSS Widget
add_action('admin_menu', 'custom_css_hooks');
add_action('save_post', 'save_custom_css');
add_action('wp_head','insert_custom_css');
function custom_css_hooks() {
	add_meta_box('custom_css', 'Custom CSS', 'custom_css_input', 'post', 'normal', 'high');
	add_meta_box('custom_css', 'Custom CSS', 'custom_css_input', 'page', 'normal', 'high');
}
function custom_css_input() {
	global $post;
	echo '&lt;input type="hidden" name="custom_css_noncename" id="custom_css_noncename" value="'.wp_create_nonce('custom-css').'" /&gt;';
	echo '&lt;textarea name="custom_css" id="custom_css" rows="5" cols="30" style="width:100%;"&gt;'.get_post_meta($post-&gt;ID,'_custom_css',true).'&lt;/textarea&gt;';
}
function save_custom_css($post_id) {
	if (!wp_verify_nonce($_POST['custom_css_noncename'], 'custom-css')) return $post_id;
	if (defined('DOING_AUTOSAVE') &amp;&amp; DOING_AUTOSAVE) return $post_id;
	$custom_css = $_POST['custom_css'];
	update_post_meta($post_id, '_custom_css', $custom_css);
}
function insert_custom_css() {
	if (is_page() || is_single()) {
		if (have_posts()) : while (have_posts()) : the_post();
			echo '&lt;style type="text/css"&gt;'.get_post_meta(get_the_ID(), '_custom_css', true).'&lt;/style&gt;';
		endwhile; endif;
		rewind_posts();
	}
}</code></pre>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/blog-images/csspanel.png" width="368" height="236" alt="" title="" /><br />
Custom CSS Write Panel
</div>
<p>As written, it&#8217;s more limiting than the Art Direction plugin as it is for CSS only rather than &#8220;anything&#8221; (e.g. JavaScript), although that would be a fairly trivial adjustment to what gets echoed out. The Art Direction plugin also allowed the CSS to be applied in other places the post might be output, for example, archives pages, whereas this does not. But to be honest, I never used that anyway.</p>
<h3>And so&#8230;</h3>
<p>Two pretty sweet and totally function solutions by Digging Into WordPress readers. Awesome. Huge thanks to Hassan and Kerrick! I would also love to see the Art Direction plugin updated as well, if nothing else, because I&#8217;m sure people download that plugin every single day from the plugin repository and have caching going on their blogs and end up in the same sore spot I was.</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/02/custom-css-per-post/">Permalink</a> | <a href="http://digwp.com/2010/02/custom-css-per-post/#comments">29 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/02/custom-css-per-post/&title=Custom CSS Per Post">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/css/" rel="tag">CSS</a>, <a href="http://digwp.com/tag/design/" rel="tag">Design</a>, <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/header/" rel="tag">header</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/02/custom-css-per-post/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>

