Is there any way to add the .html
extension to custom post types without plugin ?
For posts I can use /%postname.html
on the permalink settings
For pages I can use:
add_action('init', 'change_page_permalink', -1);
function change_page_permalink() {
global $wp_rewrite;
if ( strstr($wp_rewrite->get_page_permastruct(), '.html') != '.html' )
$wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
}
For Custom post types ???
Is there any piece of code similar to the one above that allws me to change or to add the .html
on the custom post type url ?
This seem to work:
Create the rewrite rules like post-type/post-name.html
. You can use arrays to create the rules for just some set of post types instead of doing it for all of them.
add_action( 'rewrite_rules_array', 'rewrite_rules' );
function rewrite_rules( $rules ) {
$new_rules = array();
foreach ( get_post_types() as $t )
$new_rules[ $t . '/([^/]+)\.html$' ] = 'index.php?post_type=" . $t . "&name=$matches[1]';
return $new_rules + $rules;
}
Format the new permalink structure for these post types.
add_filter( 'post_type_link', 'custom_post_permalink' ); // for cpt post_type_link (rather than post_link)
function custom_post_permalink ( $post_link ) {
global $post;
$type = get_post_type( $post->ID );
return home_url( $type . "https://wordpress.stackexchange.com/" . $post->post_name . '.html' );
}
And then stop redirecting the canonical URLs to remove the trailing slash. This might need some more work, as you’ll probably want to keep the redirection for most cases.
add_filter( 'redirect_canonical', '__return_false' );
As others said around here, after doing the above you’ll need to flush the rules, and that’s possible by visiting the options-permalink.php
admin page in Dashboard -> Settings -> Permalinks
.