Blogless: Blog of Design Less Better.

Posts tagged .

Page-specific CSS and Javascript using Wordpress Custom Fields

Use this PHP snippet and Wordpress custom fields to reference page-specific CSS and Javascript files in your Wordpress theme.

DLB's latest project is a website with some content-complexity, using Wordpress as a Content Management System. Wordpress is functional as a custom CMS largely because of Custom Fields, which allow you to assign an arbitrary amount of meta-data to a post (the core element of a Wordpress site).

Now, on this website, it came about that some of the pages needed specific Javascript classes and methods, and even more than that needed custom CSS classes.

Immediately, it occurred to me that this could be handled through custom fields. So, what I did, and you can do, too, is create two custom fields: one called custom_css and one called custom_js.

Setting up the custom fields in Wordpress
Put the fully qualified URI of the file into a custom field.

With this accomplished, edit your Wordpress theme's header.php, and write a couple little PHP conditionals to check for this field. Or else, just copy and paste mine!

These icons link to social bookmarking sites where readers can share and discover new web pages.
PaulAug 11, 2008
 
Close this
E-mail It