DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Sometimes, a specific post needs to be styled with some custom css styles. Sure, you can directly embed CSS in your post, but that not very clean. In this recipe, I’m going to show you how to embed css in a clean way, by using a custom field.

First, open your header.php file and insert the following code between the <head> and </head> html tags:

<?php if (is_single()) {
    $css = get_post_meta($post->ID, 'css', true);
    if (!empty($css)) { ?>
        <style type="text/css">
        <?php echo $css; ?>   
    <?php } 
} ?>

Once done, when you’re writing a post or page which require some custom css styling, you’ll just have to create a custom field named css and paste your custom css styles as a value. That’s simple as that!


  1. Grrrr…. I can modify all elements : h1, font-family, etc. But i have a problem with the background : i can change the color but i can’t change the background-image. I don’t understand why…

    body{background:red url(‘http://wallpapers-diq.com/wallpapers/12/Abstract_Silver_Myspace_Background.jpg’) left top repeat !important;}

    An idea to help me?

  2. @Stef

    Is that your website or are you trying to pull it from another website? I tested it using various methods (using custom fields, modifying actual theme’s style.css, started from scratch with a blank html) and the code worked fine. However, your image didn’t show up for me either, which leads me to believe that the website hosting the image does not allow for external linking. Try downloading and uploading to a different location. Then you should be fine.

  3. Hi Michael,

    You’re alright, It works. I gave you this image just for the exemple. You can see the result on my website :-)


  4. That worked great – thanks!

    I’m using the same css value on all posts for this post-specific css (“display:none;” to hide some elements) and I’m wondering if there is a way to do this via a checkbox instead of adding the css for every post again.

    Any help would be highly appreciated!


  5. So, with this technique will i have to insert … tags with some style and then add standard HTML tags within the post for the CSS to work? Thank you!

  6. Hi, thanks for this!
    I can’t get it to work tho. I have the custom field “css”, and the input value “.section:{background:red;}”. Am I completely off here?:)

  7. Does not seem to work with WP 3.5 and up

Leave a Comment

Your email address will not be published. Required fields are marked *