ionfish / designate

WordPress plugin allowing per-post stylesheets.

This URL has Read+Write access

commit  1151d741b37afbedcafc03dd35b0b42a64b7288e
tree    1cb5d0aacc1008b9de537868a41c7d3bd87b52a4
parent  81af830cf5bc1f823d527e7f28e5b21804d7c970
designate / designate.php
100644 56 lines (49 sloc) 1.551 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/*
Plugin Name: Designate
Plugin URI: http://github.com/ionfish/designate/
Description: Add a per-post stylesheet to customise every post.
Author: Benedict Eastaugh
Version: 1.0
Author URI: http://extralogical.net/
*/
 
/**
* Set a custom stylesheet for each post or page.
*
* Stylesheets should be added in the 'post-styles' directory in your
* WP_CONTENT_DIR.
*
* You can override the programmatically-generated stylesheet name by adding a
* custom field to your post or page with the key 'stylesheet' and the
* stylesheet name (e.g., 'my_style.css') as the value.
*
* Please note that if two posts have the same permalink slug, they will have
* the same stylesheet. If several of your posts which you wish to style have
* the same permalink, set the $use_ids parameter to true.
*
* @param boolean $use_ids
*/
function designate_stylesheet($use_ids = false) {
global $post;
 
if (is_home() && have_posts() || is_single() || is_page()) {
$custom = get_post_meta($post->ID, 'stylesheet', true);
 
if ($custom && strlen($custom) > 0) {
$slug = preg_replace('/\.css$/', '', $custom);
} elseif ($post->post_name && !$use_ids) {
$slug = $post->post_name;
} else {
$slug = 'post-style-' . $post->ID;
}
 
if (strlen($slug) > 0) {
$location = "/post-styles/$slug.css";
 
if (file_exists(WP_CONTENT_DIR . $location)) {
printf(
"<link type=\"text/css\" rel=\"stylesheet\" href=\"%s\" />\n\n",
content_url($location)
);
}
}
}
}
 
add_action('wp_head', 'designate_stylesheet');
 
?>