`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 are 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 SiteTreeImageElement objects.
    $images = array(
        SiteTreeImageElement::make( 'https://sitetreeplugin.com/image-1.jpg', 'Image Title', 'Image caption.' ),
        SiteTreeImageElement::make( 'https://sitetreeplugin.com/image-2.jpg', 'Image Title', 'Image caption.' ),
        SiteTreeImageElement::make( 'https://sitetreeplugin.com/image-3.jpg', 'Image Title', 'Image caption.' )
    );

    $sitemapBuilder->buildURLElement( 'https://sitetreeplugin.com/', '2020-03-23', $images );
}

add_action( 'sitetree_is_building_sitemap', 'add_url_element_to_sitemap' );

The $sitemapBuilder parameter is a reference to the object of class SiteTreeSitemapBuilder to which is assigned the task of building the Google Sitemap. Its class' interface, declared in core/builders/builders-interfaces.php, is the following:

interface SiteTreeSitemapBuilderInterface {
    /**
     * Limit set according to the XML schema defined for 
     * the Image Sitemap extension.
     *
     * @since 1.0
     */
    const IMAGES_PER_URL_ELEMENT = 1000;

    /**
     * @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}.
     * @param array $images       Optional. Array of SiteTreeImageElement objects, {@see SiteTreeImageElementInterface}.
     *                            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 SiteTreeImageElement is declared in core/builders/image-element.class.php as follows:

interface SiteTreeImageElementInterface {
    /**
     * Returns a SiteTreeImageElement object.
     *
     * @since 1.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: