`sitetree_is_building_sitemap`

sitetree_is_building_sitemap is an action triggered whenever the Google Sitemap is rebuilt, after all the Posts, Pages and Custom Posts elements have been generated, but before the taxonomy elements are created.

Adding <url> Elements to the Google Sitemap

The code below adds a <url> element with <image> child elements to the Google Sitemap.

function add_url_element_to_sitemap( $sitemapBuilder ) {
    // 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/', '2020-11-24', $images );
}

add_action( 'sitetree_is_building_sitemap', 'add_url_element_to_sitemap' );

The $sitemapBuilder parameter is a reference to the object of class SiteTree\SitemapBuilder to which is assigned the task of building the Google Sitemap. Its class' interface, 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: