Last active
December 15, 2015 07:19
-
-
Save gglnx/5222998 to your computer and use it in GitHub Desktop.
Changes the permalink for a custom post type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Custom URIs for Podlove Publisher | |
* Description: Changes the URIs for the episode custom post type of the Podlove Publisher | |
* Author: Dennis Morhardt | |
* Version: 1.0 | |
* Author URI: http://www.dennismorhardt.de/ | |
*/ | |
/** | |
* Removes the rewrite flag from the podlove custom post type | |
* | |
* @since 1.0.0 | |
*/ | |
add_filter('podlove_post_type_args', function($args) { | |
$args['rewrite'] = false; | |
return $args; | |
}); | |
/** | |
* Changes the permalink for a custom post type | |
* | |
* @since 1.0.0 | |
* @uses $wp_post_types | |
* @uses $wp_rewrite | |
*/ | |
function register_new_custom_rewrite() { | |
global $wp_post_types, $wp_rewrite; | |
foreach ( $wp_post_types as $post_type => $options ): | |
if ( $post_type != "podcast" ) continue; | |
$wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', "post_type=$post_type&name="); | |
$wp_rewrite->add_permastruct($post_type, "%year%%monthnum%/%$post_type%", false, EP_PERMALINK); | |
endforeach; | |
} | |
add_action('init', 'register_new_custom_rewrite', 99); | |
/** | |
* Replace %year% and %monthnum% in permalinks with the correct values | |
* | |
* @since 1.0.0 | |
*/ | |
function filter_post_type_link($post_link, $id) { | |
$post = &get_post($id); | |
$unixtime = strtotime($post->post_date); | |
$post_link = str_replace('%year%', date('Y', $unixtime), $post_link); | |
$post_link = str_replace('%monthnum%', date('m', $unixtime), $post_link); | |
return $post_link; | |
} | |
add_filter('post_type_link', 'filter_post_type_link', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment