-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrewrite.php
More file actions
126 lines (107 loc) · 3.28 KB
/
rewrite.php
File metadata and controls
126 lines (107 loc) · 3.28 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
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
/**
* Check for the existence of the rewrite rules on each page load and flushes
* the rules if they do not exist.
*
* @author Castlgate IT <info@castlegateit.co.uk>
* @author Andy Reading
*
* @return void
*/
function cgit_wp_events_apply_rules()
{
global $wp_rewrite;
// Get the post type for the archive slug and check it has an archive
$post_type = get_post_type_object(CGIT_EVENTS_POST_TYPE);
if (!$post_type->has_archive) {
return;
}
// Check for the main rewrite rule and flush if it does not exist
$check_rule = $post_type->rewrite['slug'] . '/?$';
$rules = $wp_rewrite->wp_rewrite_rules();
if ($rules && in_array($check_rule, array_keys($rules))) {
cgit_wp_events_flush_rules();
}
}
add_filter('init', 'cgit_wp_events_apply_rules');
/**
* Setup date archives rewrite rules for the custom post type.
*
* @param array $existing_rules Existing WP rewrite rules
*
* @author Castlgate IT <info@castlegateit.co.uk>
* @author Andy Reading
*
* @return array
*/
function cgit_wp_events_generate_archives($existing_rules)
{
global $wp_rewrite;
$rules = array();
// Get the post type for the archive slug
$post_type = get_post_type_object(CGIT_EVENTS_POST_TYPE);
$slug_archive = $post_type->has_archive;
// The post type always has an archive, but include this check anyway
if ($slug_archive === false) {
return $rules;
}
// Define the archive slug if it's not already defined
if ($slug_archive === true) {
$slug_archive = $post_type->rewrite['slug'];
if (!$post_type->rewrite['slug']) {
$slug_archive = $post_type->name;
}
}
// Build an array of regex rules and their query vars
$dates = array(
array(
'rule' => "([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})",
'vars' => array('year', 'monthnum', 'day')
),
array(
'rule' => "([0-9]{4})/([0-9]{1,2})",
'vars' => array('year', 'monthnum')
),
array(
'rule' => "([0-9]{4})",
'vars' => array('year')
)
);
// Process each rewrite rule
foreach ($dates as $data) {
$query = 'index.php?post_type=' . CGIT_EVENTS_POST_TYPE;
$rule = $slug_archive . '/' . $data['rule'];
$i = 1;
foreach ($data['vars'] as $var) {
$query.= '&' . $var . '=$matches[' . $i . ']';
$i++;
}
// The completed rules
$regex = $rule . '/?$';
$rules[$regex] = $query;
// Rules for feeds
$regex = $rule . '/feed/(feed|rdf|rss|rss2|atom)/?$';
$rules[$regex] = $query . '&feed=$matches[' . $i . ']';
$regex = $rule . '/(feed|rdf|rss|rss2|atom)/?$';
$rules[$regex] = $query . '&feed=$matches[' . $i . ']';
// Rules for pages
$regex = $rule . '/page/([0-9]{1,})/?$';
$rules[$regex] = $query.'&paged=$matches[' . $i . ']';
}
// Return the rules
return $rules + $existing_rules;
}
add_filter('rewrite_rules_array', 'cgit_wp_events_generate_archives');
/**
* Flush the rewrite rules
*
* @author Castlgate IT <info@castlegateit.co.uk>
* @author Andy Reading
*
* @return void
*/
function cgit_wp_events_flush_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}