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 ?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 . '/' . $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.
Method 2
You could add a rewrite rule for this that supersedes the built-in permalinks, e.g. for a custom post type “product”…
add_action('init', 'add_html_ext_to_custom_post_types');
function add_html_ext_to_custom_post_types() {
add_rewrite_rule('^product/([^/]+).html', 'index.php?product=$matches[1]', 'top');
}
(Don’t forget to flush your rules either by re-saving your permalinks or using flush_rules in the way @toscho notes above).
Caveats
- I don’t think functions like
the_permalink()would use this, so you might have to add a filter forpost_linkto catch those links. You could also add to theredirect_canonicalfilter to redirect the default permalinks, so that /product/foo and /product/foo/ redirect to /product/foo.html. - You would need to add addition rewrites for other URLs that your site used, like feed URLs, subsequent pages, trackbacks, etc. The code above would just work for the main Custom Post Type page.
Method 3
If you would prefer a WordPress plugin to handle the work for you, check out
Custom Post Type Permalinks in the WordPress plugin repository. Tested on WordPress 3.4.1 and it works perfectly.
After activating the plugin, just navigate to Dashboard -> Settings -> Permalinks.
You can add specific rewrites for each registered custom post type.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0