-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjalali-permalinks.php
46 lines (36 loc) · 1.58 KB
/
jalali-permalinks.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
<?php
/**
* Plugin Name: Jalali Permalinks
* Description: Enables the Shamsi month name tag for permalinks.
* Author: Mohammad Reza Tayyebi <github@tyyi.net>
* License: GPLv2
*/
// example: /%jyear%-%jmonthname%/%postname%/
define('MY_PLUGIN_DIR', plugin_dir_path(__FILE__));
include MY_PLUGIN_DIR . 'parsidate.php';
class JalaliPermalinks {
public static $monthnames = array(
'farvardin', 'ordibehesht', 'khordad', 'tir', 'mordad', 'shahrivar',
'mehr', 'aban', 'azar', 'dey', 'bahman', 'esfand'
);
public static function init() {
add_rewrite_tag('%jmonthname%', '(' . implode('|', self::$monthnames) . ')');
add_rewrite_rule('^([0-9]{4})-(' . implode('|', self::$monthnames) . ')/([^/]+)/?', 'index.php?name=$matches[3]', 'top');
}
public static function filter_post_link($permalink, $post) {
// if (false === strpos($permalink, '%j')) {
// return $permalink;
// }
$gregorian_date = get_the_date('Y-m-d', $post);
$parsidate = new bn_parsidate();
$jmonthname_persian = strtolower($parsidate->persian_date('F', $gregorian_date, 'per'));
$jyear = $parsidate->persian_date('Y', $gregorian_date, 'en');
$monthindex = intval(get_post_time('n', false, $post->ID));
$jmonthindex = $parsidate->persian_date('m', $gregorian_date, 'en');
$monthname = self::$monthnames[intval($jmonthindex) - 1];
$permalink = str_replace('%jmonthname%', $monthname, $permalink);
$permalink = str_replace('%jyear%', $jyear, $permalink);
return $permalink;
}
}
add_action('init', array('JalaliPermalinks', 'init'));