tpc_is_building_sitemap is an action triggered whenever a Google Sitemap is rebuilt, after all the elements of a standard Google Sitemap have been generated.

Adding <url> Elements to a Google Sitemap

The code below adds a <url> element to the first Google Sitemap generated for the 'page' content type.

function add_url_element_to_first_page_sitemap( $sitemapBuilder, $indexer ) {
    if ( $indexer->getRequestedSitemapUID() == 'page-1' ) {
        $sitemapBuilder->buildURLElement( 'https://luigicavalieri.com/', '2021-09-21' );

add_action( 'tpc_is_building_sitemap', 'add_url_element_to_first_page_sitemap', 10, 2 );

The $sitemapBuilder parameter is a reference to the object of class ThePermalinksCascade\SitemapBuilder to which is assigned the task of building a Google Sitemap, whereas the $indexer parameter is a reference to the object in charge of creating and managing the index of sitemaps.

The class interface of the $sitemapBuilder, declared in includes/builders/builders-interfaces.php, is the following:

interface SitemapBuilderInterface {
     * @since 1.0
     * @param string $url         Absolute URL of a publicly accessible web page.
     * @param string|int $lastmod Optional. The date on which the page was last modified or the timestamp of said date.
     *                            Valid date formats at {@link https://www.php.net/manual/en/datetime.formats.php}.    
    public function buildURLElement( $url, $lastmod = '' );
Last update: