forked from ryanhellyer/unique-headers-single-posts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique-headers-single-posts.php
126 lines (99 loc) · 3.18 KB
/
unique-headers-single-posts.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/*
Plugin Name: Unique Headers Single Posts
Plugin URI: https://geek.hellyer.kiwi/plugins/unique-headers-single-posts/
Description: Forces single posts pages to grab the first category from a post and use it's header image for that post
Version: 1.5
Author: Ryan Hellyer
Author URI: https://geek.hellyer.kiwi/
------------------------------------------------------------------------
Copyright Ryan Hellyer 2016
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Do not continue processing since file was called directly
*
* @since 1.0
* @author Ryan Hellyer <[email protected]>
*/
if ( !defined( 'ABSPATH' ) ) {
die( 'Eh! What you doin in here?' );
}
/**
* Set single post header images to use same header as category
*
* @copyright Copyright (c), Ryan Hellyer
* @license http://www.gnu.org/licenses/gpl.html GPL
* @author Ryan Hellyer <[email protected]>
* @since 1.0
*/
class Single_Post_Header_Images {
/**
* Class constructor
*/
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
/**
* Print styles to admin page
*/
public function init() {
if ( ! class_exists( 'Unique_Headers_Taxonomy_Header_Images' ) ) {
return;
}
// Add filters
add_filter( 'theme_mod_header_image', array( $this, 'header_image_filter' ) );
}
/*
* Filter for modifying the output of get_header()
*/
public function header_image_filter( $url ) {
global $post;
// Bail out now if not in category
if ( ! is_single() ) {
return $url;
}
// Loop through all taxonomies
$taxonomies = get_taxonomies( array( 'public' => true ) );
if ( is_array( $taxonomies ) ) {
foreach( $taxonomies as $x => $taxonomy ) {
if ( ! isset( $url_set ) ) {
// Loop through terms in the taxonomy
$terms = wp_get_post_terms( get_the_ID(), $taxonomy );
$count = 0;
foreach ( $terms as $term ) {
// Only bother doing the first 10 terms (avoids overloading the database)
if ( $count < 10 && ! isset( $url_set ) ) {
$term_id = $term->term_id;
// Grab stored taxonomy header
$meta = get_term_meta( $term_id, 'taxonomy-header-image', true );
if ( is_numeric( $meta ) ) {
$attachment = wp_get_attachment_image_src( $meta, 'full' );
$url = $attachment[0];
$url_set = true;
} elseif ( '' != $meta ) {
$url = $meta; // Handling legacy URL's from earlier versions of the plugin
$url_set = true;
} else {
// Leave image URL as is
}
}
$count++;
}
}
}
}
return $url;
}
}
new Single_Post_Header_Images;