-
Notifications
You must be signed in to change notification settings - Fork 144
/
post-stylesheets.php
67 lines (56 loc) · 2.88 KB
/
post-stylesheets.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
<?php
/**
* Post Stylesheets - A WordPress script for post-specific stylesheets.
*
* Post Stylesheets allows users and developers to add unique, per-post stylesheets. This script was
* created so that custom stylesheet files could be dropped into a theme's '/css' folder and loaded for
* individual posts using the 'Stylesheet' post meta key and the stylesheet name as the post meta value.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License version 2, as published by the Free Software Foundation. You may NOT assume
* that you can use any other version of the GPL.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @package PostStylesheets
* @version 0.1.0
* @author Justin Tadlock <justin@justintadlock.com>
* @copyright Copyright (c) 2010, Justin Tadlock
* @link http://justintadlock.com
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/* Filters stylesheet_uri with a function for adding a new style. */
add_filter( 'stylesheet_uri', 'post_stylesheets_stylesheet_uri', 10, 2 );
/**
* Checks if a post (or any post type) has the given meta key of 'Stylesheet' when on the singular view of
* the post on the front of the site. If found, the function checks within the '/css' folder of the stylesheet
* directory (child theme) and the template directory (parent theme). If the file exists, it is used rather
* than the typical style.css file.
*
* @since 0.1.0
*/
function post_stylesheets_stylesheet_uri( $stylesheet_uri, $stylesheet_dir_uri ) {
global $wp_query;
/* Check if viewing a singular post. */
if ( is_singular() ) {
/* Allow plugin/theme developers to override the default meta key. */
$meta_key = apply_filters( 'post_stylesheets_meta_key', 'Stylesheet' );
/* Get the post ID. */
$post_id = $wp_query->get_queried_object_id();
/* Check if the user has set a value for the post stylesheet. */
$stylesheet = get_post_meta( $post_id, $meta_key, true );
/* If a meta value was given and the file exists, set $stylesheet_uri to the new file. */
if ( !empty( $stylesheet ) ) {
/* If the stylesheet is found in the child theme '/css' folder, use it. */
if ( file_exists( trailingslashit( get_stylesheet_directory() ) . "css/{$stylesheet}" ) )
$stylesheet_uri = trailingslashit( $stylesheet_dir_uri ) . "css/{$stylesheet}";
/* Else, if the stylesheet is found in the parent theme '/css' folder, use it. */
elseif ( file_exists( trailingslashit( get_template_directory() ) . "css/{$stylesheet}" ) )
$stylesheet_uri = trailingslashit( get_template_directory_uri() ) . "css/{$stylesheet}";
}
}
/* Return the stylesheet URI. */
return $stylesheet_uri;
}
?>