Creating a new custom sitemap

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

    Thank you for getting in touch with us.

    You can use the following filter to add additional URLs to the sitemap:

    
    /**
     Filter to add extra URLs to the XML sitemap by type.
     *
     Only runs for the first page, not on all.
     *
     @param string $content String content to add, defaults to empty.
     */
    add_action( 'rank_math/sitemap/{$type}_content', function() {
    return '<url>
    <loc>https://rankmath.com/some-custom-url/</loc>
    <lastmod>2020-06-10T20:20:20+00:00</lastmod>
    </url>';
    });
    

    Replace {$type} with either post or page depending on if you want the URL to be include in the post or page sitemap – respectively.

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Did you guys get a chance to look at it

    we tried adding the custom sitemap page, link was created but the link is giving 404 error. And we tried adding index as well. Below are the code snippet

    from functions.php

    add_filter('rank_math/sitemap/index', function () {
    return '<sitemap>
    <loc>' . get_home_url() . '/amp-sitemap.xml</loc>
    <lastmod>' . date(DATE_ATOM) . '</lastmod>
    </sitemap>';
    });

    In send post – where we have the dynamic URL for amp_story

    tried all these

    add_filter('rank_math/sitemap/local/content', function () {
    // Loop of tags.
    return '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
    <loc>http://localhost/tldr-wp/social-media/linkedin-rolls-out-support-reaction/</loc>
    <lastmod>' . date(DATE_ATOM) . '</lastmod>
    </url>
    </urlset>';
    });
    add_filter('rank_math/sitemap/amp_content', function () {
    // Loop of tags.
    return '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
    <loc>http://localhost/tldr-wp/social-media/linkedin-rolls-out-support-reaction/</loc>
    <lastmod>' . date(DATE_ATOM) . '</lastmod>
    </url>
    </urlset>';
    });
    add_filter( 'rank_math/sitemap/local/content', function() {
    // Loop of tags.
    return '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
    <loc>' . get_home_url() . '/amp-stories/' . date('Ymd') . '/</loc>
    <lastmod>' . date(DATE_ATOM) . '</lastmod>
    </url>
    </urlset>';
    });

    Tried adding these in ampstory page as well.

    but still did not work as expected

    Hello,

    Assuming your the sitemsp you want to add your URLs to is page-sitemap, the following it the usage of the filter:

    
    add_action( 'rank_math/sitemap/page_content', function() {
    	return '<url>
    		<loc>http://localhost/tldr-wp/social-media/linkedin-rolls-out-support-reaction/</loc>
    		<lastmod>2020-06-10T20:20:20+00:00</lastmod>
    	</url>';
    });
    

    Remember to specify the sitemap you want to add your URLs to and to keep the format to be in XML format. You can confirm if your output is actually HTML by testing an output of your code.

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Hi Michael

    As mention in my initial post

    is it possible to create a new sitemap like https://tldrmarketing.com/amp-sitemap.xml instead of adding it the pre built one like pages, posts or categories?

    Hello,

    Adding a new sitemap type is a bit more complicated than just adding extra links to an existing sitemap. Technically, in both cases Google will see and handle links exactly the same way, so the difference is mostly just aesthetic.

    Once you got the amp-sitemap.xml included in your sitemap index, you need to do these steps:

    1. Create a new file in your child theme folder (assuming you added the previous filter codes in the functions.php file of your child theme), name it custom-sitemap.php and add the following code in it:

    <?php
    
    namespace RankMath\Sitemap\Providers;
    
    defined( 'ABSPATH' ) || exit;
    
    class Custom implements Provider {
    
    	public function handles_type( $type ) {
    		return true;
    	}
    
    	public function get_index_links( $max_entries ) {
    		return [];
    	}
    
    	public function get_sitemap_links( $type, $max_entries, $current_page ) {
    		$links     = [ 
    			[ 'loc' => 'http://www.example.com/' ],
    			[ 'loc' => 'http://www.example.com/2',],
    		];
    
    		return $links;
    	}
    
    }

    2. Replace the example.com URLs and add your custom links in the get_sitemap_links() function.

    3. Add this filter code in the functions.php of your child theme:

    include_once 'sitemap-custom.php';
    add_filter('rank_math/sitemap/providers', function( $external_providers ) {
    	$external_providers['custom'] = new \RankMath\Sitemap\Providers\Custom();
    	return $external_providers;
    });

    Then, the new sitemap should show up with your custom links in it.

    Hope that helps.

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

You must be logged in to reply to this ticket.