`sitetree_is_building_sitemap`

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

Adding <url> Elements to a Google Sitemap

The code below adds a <url> element with <image> child elements 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' ) {
        // Array of SiteTree\ImageElement objects.
        $images = array(
            \SiteTree\ImageElement::make( 'https://luigicavalieri.com/image-1.jpg', 'Image Title', 'Image caption.' ),
            \SiteTree\ImageElement::make( 'https://luigicavalieri.com/image-2.jpg', 'Image Title', 'Image caption.' ),
            \SiteTree\ImageElement::make( 'https://luigicavalieri.com/image-3.jpg', 'Image Title', 'Image caption.' )
        );

        $sitemapBuilder->buildURLElement( 'https://luigicavalieri.com/', '2021-04-19', $images );
    }
}

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

The $sitemapBuilder parameter is a reference to the object of class SiteTree\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 {
    /**
     * Limit set according to the XML schema defined for 
     * the Image Sitemap extension.
     *
     * @since 5.0
     */
    const IMAGES_PER_URL_ELEMENT = 1000;

    /**
     * @since 5.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}.
     * @param array $images       Optional. Array of ImageElement objects, {@see ImageElementInterface}.
     *                            Its size must not exceed {@see self::IMAGES_PER_URL_ELEMENT}.       
     */
    public function buildURLElement( $url, $lastmod = '', $images = array(), ...$deprecated_args );
}

The interface of the class SiteTree\ImageElement is declared in includes/builders/image-element.class.php as follows:

interface ImageElementInterface {
    /**
     * Returns a ImageElement object.
     *
     * @since 5.0
     *
     * @param string $url     Absolute URL of the image file.
     * @param string $title   Optional.
     * @param string $caption Optional.
     * @return object
     */
    public static function make( $url, $title = '', $caption = '' );
}
Last update: