badcrocodile
6/19/2015 - 6:47 PM

Configuring custom routes in wordpress

Configuring custom routes in wordpress

<?php
/* ========================= */
/* == Configure our routes == */
/* ========================= */

// For month and/or year based archives
function saos_rewrite_tags() {
    add_rewrite_tag('%getyear%', '([^&]+)'); // adds year to permalinks
    add_rewrite_tag('%getmonth%', '([^&]+)'); // adds month to permalinks
//     add_rewrite_tag( '%bf_events_year%', '([0-9]{4})' ); // adds year to single page permalinks
//     add_rewrite_tag( '%bf_events_month%', '([^&]+)' ); // adds month to single page permalinks
}
add_action('init', 'saos_rewrite_tags', 10, 0);

function saos_rewrites() {
    /* -- CLE+ in the National Press (page id 41) -- */
    // for root URL (no month/year) pagination
    add_rewrite_rule('^news-press/national-press/page/([0-9]{1,})/?$','index.php?page_id=41&paged=$matches[1]','top');
    // for year and month archives, ie: http://foo.com/bar/2013/02/
    add_rewrite_rule('^news-press/national-press/([^/]*)/([^/]*)/?$','index.php?page_id=41&getyear=$matches[1]&getmonth=$matches[2]','top');
    // and for month/year with pagination, ie: http://foo.com/bar/2013/02/page/3/
    add_rewrite_rule('^news-press/national-press/([^/]*)/([^/]*)/page/([0-9]{1,})/?$','index.php?page_id=41&getyear=$matches[1]&getmonth=$matches[2]&paged=$matches[3]','top');
    // for just year archives, ie: http://foo.com/bar/2014/
    add_rewrite_rule('^news-press/national-press/([^/]*)/?$','index.php?page_id=41&getyear=$matches[1]','top');
    // and for year with pagination, ie: http://foo.com/bar/2013/page/3/
    add_rewrite_rule('^news-press/national-press/([^/]*)/page/([0-9]{1,})/?$','index.php?page_id=41&getyear=$matches[1]&paged=$matches[2]','top');
    
    /* -- "The Plus" News (page id 1581) -- */
    // for root URL (no month/year) pagination
    add_rewrite_rule('^news-press/the-plus-news/page/([0-9]{1,})/?$','index.php?page_id=1581&paged=$matches[1]','top');
    // for year and month archives, ie: http://foo.com/bar/2013/02/
    add_rewrite_rule('^news-press/the-plus-news/([^/]*)/([^/]*)/?$','index.php?page_id=1581&getyear=$matches[1]&getmonth=$matches[2]','top');
    // and for month/year with pagination, ie: http://foo.com/bar/2013/02/page/3/
    add_rewrite_rule('^news-press/the-plus-news/([^/]*)/([^/]*)/page/([0-9]{1,})/?$','index.php?page_id=1581&getyear=$matches[1]&getmonth=$matches[2]&paged=$matches[3]','top');
    // for just year archives, ie: http://foo.com/bar/2014/
    add_rewrite_rule('^news-press/the-plus-news/([^/]*)/?$','index.php?page_id=1581&getyear=$matches[1]','top');
    // and for year with pagination, ie: http://foo.com/bar/2013/page/3/
    add_rewrite_rule('^news-press/the-plus-news/([^/]*)/page/([0-9]{1,})/?$','index.php?page_id=1581&getyear=$matches[1]&paged=$matches[2]','top');
}
add_action('init', 'saos_rewrites', 10, 0);

/* -- Add month and year to single custom post type permalink -- */
function saos_custom_permalinks($permalink, $post, $leavename) {
    if(get_post_type($post) === "the-plus-news") {
        $year = date('Y', strtotime($post->post_date));
        $month = date('F', strtotime($post->post_date));
        $rewritecode = array(
            '%getyear%',
            '%getmonth%',
            $leavename ? '' : '%postname%',
        );
        $rewritereplace = array(
            $year,
            $month,
            $post->post_name
        );
        $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    }
    if(get_post_type($post) === "national-press") {
        $year = date('Y', strtotime($post->post_date));
        $month = date('F', strtotime($post->post_date));
        $rewritecode = array(
            '%getyear%',
            '%getmonth%',
            $leavename ? '' : '%postname%',
        );
        $rewritereplace = array(
            $year,
            $month,
            $post->post_name
        );
        $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    }
    return $permalink;
}
add_filter( 'post_type_link', 'saos_custom_permalinks', 10, 4 );
?>