Many Ways to Add CSS
You might be wondering why I’m talking about post-specific CSS. After all, CSS rules belong in style sheets, right? (Mostly right.)
But you can also enter CSS rules inline. Just add a block like this, either in a Custom HTML block or inside the classic editor, and the rules will apply only to this post:
<style>
body {
background-color: inherit;
}
.some-class {
font-size: 3em;
}
...and so on
</style>
And if you’re using WordPress, there’s always the Additional CSS panel in the in the Appearance Customize Screen.
So, why would we ever want or need to have post-specific CSS (aka per-post CSS)?
Reasons to Use Post-Specific CSS
Unique edge cases
Sometimes the presentation rules for a specific post are just so unique that it doesn’t make sense to include them in the main CSS file or even in the Additional CSS panel.
For example, imagine you’re creating a landing page for a very narrow and specific purpose. The presentation requirements for this landing page are unique, and it’s highly unlikely you’ll ever need those rules again.
Testing
Even if the ultimate intention is to include the code in the regular CSS file, it’s a lot more efficient to just test it from within the post than to either update the main CSS file or add the rules to the customizer.
Yes, you can and should use your browser’s devtools for CSS testing. But for complex rules and declarations, it’s nice to have that markup on hand for the next time you refresh your browser.
Embedding CSS in the post itself can be risky
If always using the classic editor in code mode is your jam, there’s no problem. But for me that’s rare.
While I do still use the classic editor a lot, I’ll often start in code mode but then switch to visual mode. That’s so I can take advantage of the TinyMCE style formatting features. (Yeah, I’ve been coding HTML since 1994, but I’m not ashamed to admit I like shortcuts from time to time.)
And here’s the thing:
If you ever switch from Code mode to Visual mode in the classic editor, your CSS at the top of the file will be destroyed because newline characters are added in.
Also, TinyMCE HTML encodes some special characters. In most cases, that’s what you want. However, if (like me) you often include code snippets in your blog posts, TinyMCE can convert them in an undesirable way:
How to implement post-specific CSS
Advanced Custom Fields: add Field Group
Here’s the setup from one of my websites:
In Advanced Custom Fields, create a field group called Post-Specific CSS. It will include only one field.
- Field Type: Textarea
- Field Label: CSS Rules
- Field Name: `post-specific_css`
- Required: No
- Instructions: Enter valid CSS rules here
- Rows: 12
Location Rules: Set the field group to appear on all post types where you want to allow post-specific CSS. Typically, that would include posts and pages
NOTE: The field name is important. It can be any valid name, but it must correspond to the meta_key that is referenced in line 14 of the function below.
That’s really all there is to the ACF setup. Each applicable post or page now has a large textarea where I can enter CSS rules that apply specifically to that individual piece of content.
Adding action to wp_head hook
Add this code to a code snippet or a file in your mu-plugins directory
add_action( 'wp_head', 'nsiteful_output_per_post_css' );
function nsiteful_output_per_post_css() {
if ( ! is_singular() ) {
return;
}
$post_id = get_queried_object_id();
if ( ! $post_id ) {
return;
}
$css = get_post_meta( $post_id, 'post-specific_css', true);
if ( empty( $css ) ) {
return;
}
echo "\n\n<style id='nsiteful-per-post-css'>\n";
echo wp_strip_all_tags( $css );
echo "\n</style>\n\n";
}
Line 1: Sets wp_head as the hook
Line 14: Stores the post-specific CSS in the variable $css
Lines 19-21: Generates the style element
The Result
That’s all there is to it. Now you should see something like this in your post editor:
Like what you see? Share with others and join my mailing list. No long-term commitment, unsubscribe any time.



Leave a Reply