Home / Admin / Scan images from Elementor Galleries and counts them for the sitemap XML
Duplicate Snippet

Embed Snippet on Your Site

Scan images from Elementor Galleries and counts them for the sitemap XML

This snippet will find new images inside the post content, which has Elementor Galleries. And it will only work for self-hosted attachments.

<10
Code Preview
php
<?php
add_filter(
	'aioseo_sitemap_images',
	/**
	 * Find new images inside the post content which has Elementor galleries.
	 * It only works for self-hosted attachments.
	 *
	 * @param  array  $images The images already found.
	 * @param  object $post   The post object
	 * @return array          All images merged with found images.
	 */
	function ( $images, $post ) {
		if (
			! is_array( $images ) ||
			! is_object( $post ) ||
			empty( $post->ID ) ||
			empty( $post->post_content )
		) {
			return $images;
		}
		// Bail if this post is not being edited under Elementor.
		if ( 'builder' !== get_post_meta( $post->ID, '_elementor_edit_mode', true ) ) {
			return $images;
		}
		preg_match_all( '/<a.+?href="(http[^"]+)"\s.+?lightbox.*?>/i', $post->post_content, $matches );
		if ( empty( $matches[1] ) ) {
			return $images;
		}
		foreach ( $matches[1] as $url ) {
			$url = wp_get_attachment_url( attachment_url_to_postid( $url ) );
			if ( $url ) {
				$images[] = $url;
			}
		}
		return array_unique( $images );
	},
	10,
	2
);

Comments

Add a Comment