How to create permalink with monthname instead of using monthnum?
Asked Answered
A

2

8

I want to redirect my blog articles like this,

http://www.example.com/blog/2014/september/03/post-name

But in wordpress it only allows me to use month number,

http://www.example.com/blog/2014/09/03/post-name.

I'm searching for this but not found anything useful. Some unanswered posts and they are not even saying, whether It is possible or not. Even in the wordpress documents there is no reference for this. I found the following code but it changes the url but not linking the post page.

<?php
/**
* Plugin Name: Month Name
* Description: Enables the <code>%monthcode%</code> and <code>%monthname%</code> tag for Permalinks.
* Author: Roger Chen
* License: GPLv2
*/

/**
* Enables use of monthname (january, june) and monthcode (jan, jun).
* Supports permalinks in the 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 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]+)/?',
        'index.php?p=$matches[3]',
        'top'
    );
    add_rewrite_rule(
        '^([0-9]{4})-(' . implode( '|', self::$monthcodes ) . ')/([0-9]+)/?',
        'index.php?p=$matches[3]',
        '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( 'MonthName', 'init' ) );
add_filter( 'post_link', array( 'MonthName', 'filter_post_link' ), 10, 2 );

Somebody please say whether it is possible or not. If possible means, can you please say a way to sort out this issue.

Agonizing answered 20/11, 2014 at 6:20 Comment(8)
As far as I know. it is not possible,.Viehmann
@Quality-Expert oh! they missed the basic functionality it seems.Agonizing
Yes may be.. as wp support only monthnum only I think.Viehmann
It will work with post_id but not post_nameCockup
@Anand Can you please say me how to make it work with post_nameAgonizing
Look into WP_Rewrite:codex.wordpress.org/Class_Reference/WP_RewriteFruity
Ok finally after a lot of head bashing with wp_rewrite.php and trying to understand the black magic :) I have figured out the solution, let me know if you are still interested and I'll post the answer.Cockup
@Anand ofcourse please post the answer.Agonizing
C
5

Ok, here's the code. It currently support permalinks of the 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 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
*/

/**
 * Based on the original code by Roger Chen (https://gist.github.com/rogerhub/8306875)
 * Plugin enables use of monthname (january, june) and monthcode (jan, jun) in permalinks
 * Supports permalinks in the 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 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'
    );       

}
/**
 * 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 );
Cockup answered 21/11, 2014 at 7:49 Comment(9)
@Anand I'm using this plugin and I'm having the same issue. I just updated the plugin to the newest version and it still isn't working. I tried your answer above as well. Please help!Mammoth
@iluvpinkerton, did you try re-saving the permalinks structure?Cockup
@anand yes, a bunch of times!Mammoth
@Anand It works if I use the permalink structure from your readme file. However, I need to have 'blog/posts' in front of it. When I change it to: 'blog/posts//%year%/%monthname%/%day%/%postname%/' then the single blog posts pull in the entire blog. Do you know why that is happening? I'd appreciate the help! Thanks!Mammoth
Hi, I'll test it out and get back to you as soon as I can.Cockup
Ok, you'll need to modify both the rewrite rules defined in function init(), change the code from add_rewrite_rule( '^([0-9]{4})/(' . implode( '|' ....... to add_rewrite_rule( '^blog/posts/([0-9]{4})/(' . implode( '|' .......Cockup
Thanks @anand! I actually had tried that. The url rewrite works, but when you go to a blog post, the theme displays the index.php file instead of single.php. See here: http://nouvelledaily.net/blog/posts/2015/may/online-classes-learn-something-newMammoth
Ok, I see the issue, you are not using a day. presumably your permalink structure is set to /blog/posts/%year%/%monthcode%/%postname% , correct?Cockup
Add the code that I have highlighted in the red block : i.imgur.com/8UoCdGJ.jpgCockup
K
0

Keep the below code in your theme functions.php and use %monthname% in permalink setting

 <?php  function custom_month_permalink_structure($permalink, $post, $leavename) {
    if ($post->post_type == 'post' && strpos($permalink, '%monthname%') !== false) {
        $month_name = date('F', strtotime($post->post_date));
        $permalink = str_replace('%monthname%', strtolower($month_name), $permalink);
    }
    return $permalink; } add_filter('post_link', 'custom_month_permalink_structure', 10, 3);

function update_get_permalink_monthname($url, $post) {
    if ($post->post_type == 'post') {
        $month_name = date('F', strtotime($post->post_date));
        $url = str_replace('%monthname%', strtolower($month_name), $url);
    }
    return $url; } add_filter('post_type_link', 'custom_month_permalink_structure', 10, 3); add_filter('get_permalink', 'update_get_permalink_monthname', 10, 2); ?>
Kale answered 6/12, 2023 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.