Skip Navigation

[Resolved] Remove Slug and add .html extension to URL

This support ticket is created 7 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 7 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#511359

I am trying to: remove the custom post type slug from the url, and add a .html extension to the end of the url

I visited this URL: hidden link

I expected to see: content of custom post type

Instead, I got: Nothing Found error. Page can't be found.

I have a child theme of blank, generatepress_child.
I have added the following functions - one to remove the custom post slug, and another to add the .html extension to the URL.

Both work independently. Either I can add the .html, or I can remove the custom post slug.
BUt when I put both functions in the functions.php, the URL shows correctly (without slug, and with .html extension), but does not show any content.

TO ADD .HTML extension

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;
}
 
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' );
}
 
add_filter( 'redirect_canonical', '__return_false' );

TO REMOVE custom post SLUG

/**
 * Remove the slug from published post permalinks. Only affect our custom post type, though.
 */

function gp_remove_cpt_slug( $post_link, $post, $leavename ) {

    if ( 'course' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }
    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    return $post_link;
}

add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 3 );

/**
 * Have WordPress match postname to any of our public post types (post, page, race)
 * All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
 * By default, core only accounts for posts and pages where the slug is /post-name/
 */

function gp_parse_request_trick( $query ) {

    // Only noop the main query
    if ( ! $query->is_main_query() )
        return;

    // Only noop our very specific rewrite rule match
    if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    // 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'page', 'course' ) );
    }
}
add_action( 'pre_get_posts', 'gp_parse_request_trick' );

Kindly suggest how to combine these 2 functions together so they can achieve both at the same time.

Thanks,

#511457

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Vinai

This question falls outside of the Toolset support policy, as it is custom code and is not specifically related to Toolset products.

However, I did take a quick look at your code, and the reason you are having a problem is that in your code to add the .html rewrites you are testing for a url which includes the custom post type slug, and then in your second block of code you remove the slug, so that the initial code no longer matches.

If this is just for a course post type then you would need to rewrite your first block of code something like this:

add_action( 'rewrite_rules_array', 'rewrite_rules' );
function rewrite_rules( $rules ) {

    $new_rules = array(
    	'([^/]+)\.html$' => 'index.php?post_type=course&name=$matches[1]'
    );
    return $new_rules + $rules;
}
  
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 );
    if ( $type == 'course' ) {
    	return home_url( $post->post_name . '.html' );    	
    } else {
    	return $post_link;
    }

}

I think that should do the trick, but please note that we cannot support this code if you run into problems with it.

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.