Sitemap adding Trailing Slashes to CPT’s they have been removed from via Hook

#113986
  • Resolved Ishan Karunaratne
    Rank Math free

    Hi,

    V1.0.53.1

    On a site that I am working on, we have removed trailing slashes for specific single posts.

    The trailing slash is removed by using the user_trailingslashit WP Hook, and checking if the $type is single.

    In your class-posts-type.php where the get_url() function is used from for sitemap links, it appears that you are adding the slashes to any single URL that is not a default post but not passing the type parameter, which bypasses our check and ignores the Hook code we have used.

    if ( ‘post’ !== $post->post_type ) {
    $url[‘loc’] = user_trailingslashit( $url[‘loc’] );
    }

    Would you be able to provide the expected type value in the user_trailingslashit() function call in your next release.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Thank you for contacting Rank Math today.

    We really appreciate the suggestion. I have forwarded this to the dev. team and we will see about the best method to handle this and incorporate it in a future update.

    For now, you can use the following filter code to remove the trailing slash from the Posts URL:

    
    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){
    	if ( 'post' === $type ) {
    		$url['loc'] = untrailingslashit( $url['loc'] );
    	}
    
    	return $url;
    }, 10, 3 );
    

    I hope that helps.

    Hi Pratik,

    I tried your suggestion, and that does not work either.

    We need to remove the trailing slashes for a few CPT’s and not all links, it appears that for the filter you suggested the $type is hardcoded as ‘post’ so it pretty much affects every link output in the sitemap if that filter is used.

    class-post-type.php
    line 208
    $url = $this->do_filter( ‘sitemap/entry’, $url, ‘post’, $post );

    Thank you and happy holidays.

    Hello

    Thank you for contacting us again and sorry for the delay.
    For now, our filter hooks can’t get a specific post type but like my colleague, we will forward this to the dev team so we can incorporate this in a future update.

    But we think we might another solution for you. Here you can use this code. This code tested in our end.

    
    /**
     * Filter if XML sitemap transient cache is enabled.
     *
     * @param boolean $unsigned Enable cache or not, defaults to true
     */
    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){
    	// This code will get the current xml url 
    	$get_current_url="https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    	$current_url = 'https://www.drugstrategies.org/treatment-center-sitemap1.xml';
    	// This section will determine if the current xml url is equal to the current_url variable
    	if ( $get_current_url === $current_url ) {
    		$url['loc'] = untrailingslashit( $url['loc'] );
    	}
    	return $url;
    }, 10, 3 );

    Make sure you disable your XML cache after you put the code above or if you got any cache plugin clear it so you will see the changes on XML.

    You can disable the Rank Math XML cache by adding this to your functions too.

    /**
     * Filter if XML sitemap transient cache is enabled.
     *
     * @param boolean $unsigned Enable cache or not, defaults to true
     */
    add_filter( 'rank_math/sitemap/enable_caching', '__return_false');

    You can delete the Rank Math cache filter after you see the changes and you satisfy with the result.

    Hope this solves your issue.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘Sitemap adding Trailing Slashes to CPT’s they have been removed from via Hook’ is closed to new replies.