-
Notifications
You must be signed in to change notification settings - Fork 0
/
month-name-permalink.php
executable file
·113 lines (95 loc) · 2.92 KB
/
month-name-permalink.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
<?php
defined('ABSPATH') or die("Direct access prohibited");
/**
* Plugin Name: Month Name Permalink
* Description: Enables use of <code>%monthcode%</code> or <code>%monthname%</code> tags in permalinks to generate a structure like <code>/2014/nov/23/post-name</code> or <code>/2014/november/23/post-name</code>
* Author: Anand Shah
* License: GPLv2
*/
/**
* Inspiration from (https://gist.github.com/rogerhub/8306875)
* Plugin enables use of monthname (january, june) and monthcode (jan, jun) in permalinks
* When using <code>%monthcode%</code>, permalinks look like: http://www.example.com/2016/nov/23/post-name
* and when using <code>%monthname%</code>, permalinks look like: http://www.example.com/2016/november/23/post-name
*/
class Month_Name_Permalink {
/**
* Month Names
*/
public static $monthnames = array(
'january',
'february',
'march',
'april',
'may',
'june',
'july',
'august',
'september',
'october',
'november',
'december',
);
/**
* Month Codes
*/
public static $monthcodes = array(
'jan',
'feb',
'mar',
'apr',
'may',
'jun',
'jul',
'aug',
'sep',
'oct',
'nov',
'dec',
);
/**
* Registers all required hooks
*/
public static function init() {
add_rewrite_tag( '%monthname%', '(' . implode('|', self::$monthnames) . ')' );
add_rewrite_tag( '%monthcode%', '(' . implode('|', self::$monthcodes) . ')' );
add_rewrite_rule(
'^([0-9]{4})/(' . implode( '|', self::$monthnames ) . ')/([0-9]{1,2})/(.*)?',
'index.php?name=$matches[4]',
'top'
);
add_rewrite_rule(
'^([0-9]{4})/(' . implode( '|', self::$monthcodes ) . ')/([0-9]{1,2})/(.*)?',
'index.php?name=$matches[4]',
'top'
);
add_rewrite_rule(
'^([^/]*)/([0-9]+)/?',
'index.php?p=$matches[2]',
'top' );
add_rewrite_rule(
'^([0-9]+)/([^/]*)/?',
'index.php?p=$matches[1]',
'top' );
}
/**
* Filters the month name and month code tags
*/
public static function filter_post_link( $permalink, $post ) {
if ( false === strpos( $permalink, '%monthname%' ) && false === strpos( $permalink, '%monthcode%' ) ) {
return $permalink;
}
try {
$monthindex = intval( get_post_time( 'n', "GMT" == false, $post->ID ) );
$monthname = self::$monthnames[$monthindex - 1];
$monthcode = self::$monthcodes[$monthindex - 1];
$permalink = str_replace( '%monthname%', $monthname, $permalink );
$permalink = str_replace( '%monthcode%', $monthcode, $permalink );
return $permalink;
} catch ( Exception $e ) {
return $permalink;
}
}
}
add_action( 'init', array( 'Month_Name_Permalink', 'init' ) );
add_filter( 'post_link', array( 'Month_Name_Permalink', 'filter_post_link' ), 10, 2 );