-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdesignate.php
69 lines (60 loc) · 2.11 KB
/
designate.php
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
57
58
59
60
61
62
63
64
65
66
67
68
69
<?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.2
Author URI: http://extralogical.net/
.
Designate is released under the GPL. Please see the LICENCE file for details.
.
*/
/**
* Set this constant to false to use post IDs rather than post slugs in
* stylesheet names.
*
* E.g., a post with an ID of 27 will have a name of post-style-27.css rather
* than arent-kittens-adorable.css.
*
* This is particularly useful when you have several posts with the same
* permalink slug.
*/
define('DESIGNATE_USE_POST_SLUGS', true);
/**
* 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 DESIGNATE_USE_POST_SLUGS constant to false.
*
* @uses get_post_meta
* @uses content_url
*/
function designate_stylesheet() {
global $post;
if (!is_single() && !is_page() && !(is_home() && have_posts())) return;
$custom = get_post_meta($post->ID, 'stylesheet', true);
if ($custom && strlen($custom) > 0)
$slug = preg_replace('/\.css$/', '', $custom);
elseif ($post->post_name && DESIGNATE_USE_POST_SLUGS === true &&
preg_match('/%postname%/', get_option('permalink_structure')))
$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');
?>