php - How to create permalink with monthname instead of using monthnum? -
i want redirect blog articles this,
http://www.example.com/blog/2014/september/03/post-name
but in wordpress allows me use month number,
http://www.example.com/blog/2014/09/03/post-name.
i'm searching not found useful. unanswered posts , not saying, whether possible or not. in wordpress documents there no reference this. found following code changes url not linking post page.
<?php /** * plugin name: month name * description: enables <code>%monthcode%</code> , <code>%monthname%</code> tag permalinks. * author: roger chen * license: gplv2 */ /** * enables use of monthname (january, june) , monthcode (jan, jun). * supports permalinks in form of /2016-nov/61742/..slug.. or /2016-november/61742/..slug.. */ class monthname { /** * 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 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]+)/?', 'index.php?p=$matches[3]', 'top' ); add_rewrite_rule( '^([0-9]{4})-(' . implode( '|', self::$monthcodes ) . ')/([0-9]+)/?', 'index.php?p=$matches[3]', 'top' ); } /** * filters month name , 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( 'monthname', 'init' ) ); add_filter( 'post_link', array( 'monthname', 'filter_post_link' ), 10, 2 );
somebody please whether possible or not. if possible means, can please way sort out issue.
ok, here's code. support permalinks of following format /2014/nov/23/post-name
or /2014/november/23/post-name
<?php /** * plugin name: month name permalink * description: enables use of <code>%monthcode%</code> or <code>%monthname%</code> tags in permalinks generate structure <code>/2014/nov/23/post-name</code> or <code>/2014/november/23/post-name</code> * author: anand shah * license: gplv2 */ /** * based on original code roger chen (https://gist.github.com/rogerhub/8306875) * plugin enables use of monthname (january, june) , monthcode (jan, jun) in permalinks * supports permalinks in form of /2014/nov/23/post-name or /2014/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 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' ); } /** * filters month name , 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 );
Comments
Post a Comment