I already have found a working solution for my question, but the problem is that it neutralizes another function that adds the category base into the custom post type permalinks, and I need boths, so I must to merge them somehow. How to do that?
This is the code that adds the .html extention to the custom post types:
//Create the rewrite rules like post-type/post-name.html
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.
add_filter( 'redirect_canonical', '__return_false' );
And this is the code that adds the category base to a cources custom post type (see a similar solution, and another):
//Change your rewrite to add the course query var:
'rewrite' => array('slug' => 'courses/%course%')
//Then filter post_type_link to insert the selected course into the permalink:
function wpa_course_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'course' );
if( $terms ){
return str_replace( '%course%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_course_post_link', 1, 3 );