{"id":290,"date":"2020-08-14T15:28:19","date_gmt":"2020-08-14T15:28:19","guid":{"rendered":"https:\/\/demo.wpartisan.net\/developmentsite\/how-to-create-image-xml-sitemap-for-post-page-and-custom-post-type-in-wordpress\/"},"modified":"2024-11-05T03:27:05","modified_gmt":"2024-11-05T03:27:05","slug":"how-to-create-image-xml-sitemap-for-post-page-and-custom-post-type-in-wordpress","status":"publish","type":"post","link":"https:\/\/demo.wpartisan.net\/developmentsite\/how-to-create-image-xml-sitemap-for-post-page-and-custom-post-type-in-wordpress\/","title":{"rendered":"How to create image XML sitemap for posts, pages and Custom Post Type(CPT) in WordPress?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">To generate\/create Image XML Sitemap in WordPress properly, we need to use an action hook called publish_{$post->post_type} ( triggered whenever a post, page or Custom Post Type(CPT) is updated, or its new status is &#8220;publish&#8221; ).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before seeing the complete code, we need to know about XML image sitemap required tags and its definition:<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;markup\\&quot;\" class=\"language-\\&quot;markup\\&quot;\">&lt;image:image&gt;(required) =&gt; Encloses all information about a single image. Each &lt;url&gt; tag can contian up to 1000 &lt;image:image&gt; tags.\n&lt;image:loc&gt;(required) =&gt; The URL for the image.\n&lt;image:caption&gt;(optional) =&gt; The caption of the image.\n&lt;image:title&gt;(optional) =&gt; The title of the image.\n&lt;image:geo_location&gt;(optional) =&gt; The geographic location of the image.\n&lt;image:license&gt;(optional) =&gt; A URL to the license of the image.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I generated an image sitemap using posts, pages and Custom Post Type( CPT ) featured images and the images from the post content. I used a regular expression to get images from post content.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the complete code to create an image XML Sitemap for post, page and Custom Post Type(CPT) in WordPress:<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">add_action( 'init', 'wpartisan_xml_image_sitemap_initialization' );\n\nfunction wpartisan_xml_image_sitemap_initialization(){\n\tglobal $all_post_type_names;\n\t$default_posts_types = array(\n\t\t'page' => 'page',\n\t\t'post' => 'post',\n\t);\n\t$args = array(\n\t\t'public'   => true,\n\t\t'_builtin' => false,\n\t);\n\t$custom_post_types = get_post_types( $args, 'names' );\n\t$all_post_types = array_merge( $default_posts_types, $custom_post_types );\n\t$all_post_type_names = array_keys( $all_post_types );\n\t\n\tif( !empty( $all_post_type_names ) ) {\n\t\tforeach( $all_post_type_names as $post_type_name ):\n\t\t\tadd_action( 'publish_'.$post_type_name, 'xml_image_sitemap', 10, 2 );\n\t\tendforeach;\n\t}\n}\n\nfunction xml_image_sitemap( $ID, $post ) {\n\t\n\tglobal $all_post_type_names;\n\n\t$sitemap = '&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>';\n\t$sitemap .= '&lt;urlset xmlns=\"http:\/\/www.sitemaps.org\/schemas\/sitemap\/0.9\" xmlns:image=\"http:\/\/www.google.com\/schemas\/sitemap-image\/1.1\">';\n\t\n\tforeach ( $all_post_type_names as $pty_name ) :\n\t\n\t\tif( $pty_name === 'page' ):\n\t\t\t$args = array(\n\t\t\t\t'sort_order' => 'desc',\n\t\t\t\t'sort_column' => 'post_modified',\n\t\t\t\t'hierarchical' => 1,\n\t\t\t\t'exclude' => '',\n\t\t\t\t'include' => '',\n\t\t\t\t'meta_key' => '',\n\t\t\t\t'meta_value' => '',\n\t\t\t\t'authors' => '',\n\t\t\t\t'child_of' => 0,\n\t\t\t\t'parent' => -1,\n\t\t\t\t'exclude_tree' => '',\n\t\t\t\t'number' => '',\n\t\t\t\t'offset' => 0,\n\t\t\t\t'post_type' => ''.$pty_name.'',\n\t\t\t\t'post_status' => 'publish'\n\t\t\t); \n\t\t\t$posts = get_pages($args); \n\t\telse:\n\t\t\t$args = array(\n\t\t\t\t'numberposts' => -1,\n\t\t\t\t'orderby'     => 'modified',\n\t\t\t\t'order'       => 'DESC',\n\t\t\t\t'exclude'     => '',\n\t\t\t\t'post_type'   => ''.$pty_name.'',\n\t\t\t);\n\t\t\t$posts = get_posts($args); \n\t\tendif;\n\t\n\t\tif ( !empty( $posts ) ) {\n\t\t\tforeach( $posts as $post ) {\n\t\t\t\tsetup_postdata($post);\n\t\t\t\t$page_url = get_permalink( $post->ID );\n\t\t\t\t\n\t\t\t\t$content = $post->post_content;\n\t\t\t\t$regex = '\/src=\"([^\"]*)\"\/';\n\t\t\t\tpreg_match_all( $regex, $content, $matches );\n\t\t\t\t$matches = array_reverse($matches);\n\n\t\t\t\tif ( has_post_thumbnail( $post->ID ) || !empty( $matches[0] ) ) {\n\t\t\t\t\t\n\t\t\t\t\t$img = '';\n\t\t\t\t\t\n\t\t\t\t\tif( has_post_thumbnail( $post->ID ) ){\n\t\t\t\t\t\n\t\t\t\t\t\t$title = get_post(get_post_thumbnail_id($post->ID))->post_title;\n\t\t\t\t\t\n\t\t\t\t\t\t$img .= '&lt;image:image>' . \"\\n\";\n\t\t\t\t\t\t\t$img .= '&lt;image:loc>' . get_the_post_thumbnail_url( $post->ID ) . '&lt;\/image:loc>' . \"\\n\";\n\t\t\t\t\t\t\t$img .= '&lt;image:title>' . esc_html( $title ) . '&lt;\/image:title>' . \"\\n\";\n\t\t\t\t\t\t$img .= '&lt;\/image:image>' . \"\\n\";\n\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tif( !empty( $matches[0] ) ){\n\t\t\t\t\t\tforeach( $matches[0] as $content_image_url ){\n\t\t\t\t\t\t\t$img .= '&lt;image:image>' . \"\\n\";\n\t\t\t\t\t\t\t\t$img .= '&lt;image:loc>' . $content_image_url . '&lt;\/image:loc>' . \"\\n\";\n\t\t\t\t\t\t\t$img .= '&lt;\/image:image>' . \"\\n\";\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\t$sitemap .= \"&lt;url>\\n\";\n\t\t\t\t\t$sitemap .= \"&lt;loc>\" . $page_url . \"&lt;\/loc>\\n\";\n\t\t\t\t\t$sitemap .= $img;\n\t\t\t\t\t$sitemap .= \"&lt;\/url>\\n\";\n\t\t\t\t\n\t\t\t\t}\n\n\t\t\t}\n\t\t}\n\tendforeach;\n\t$sitemap .= '&lt;\/urlset>';\n\t$fp = fopen(ABSPATH . \"sitemap-media.xml\", 'w');\n\tfwrite($fp, $sitemap);\n\tfclose($fp);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">XML Sitemap example image<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large \\&quot;wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"427\" src=\"https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/08\/image-sitemap-1536x640-1-1024x427.png\" alt=\"\" class=\"wp-image-2305\" srcset=\"https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/08\/image-sitemap-1536x640-1-1024x427.png 1024w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/08\/image-sitemap-1536x640-1-300x125.png 300w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/08\/image-sitemap-1536x640-1-768x320.png 768w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/08\/image-sitemap-1536x640-1.png 1536w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>To generate\/create an Image XML Sitemap using a featured image and images from the post content area in WordPress, we need to use an action hook called publish_{$post->post_type} ( triggered whenever a post, page or Custom Post Type(CPT) post is updated, or its new status is &#8220;publish&#8221; ).<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15,25,18],"tags":[26,27,19,28],"class_list":["post-290","post","type-post","status-publish","format-standard","hentry","category-php","category-woocommerce","category-wordpress","tag-image-sitemap","tag-sitemap","tag-wp-sitemap","tag-xml-image-sitemap"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"WPArtisan","author_link":"https:\/\/demo.wpartisan.net\/developmentsite\/author\/developmentsiteauth\/"},"uagb_comment_info":0,"uagb_excerpt":"To generate\/create an Image XML Sitemap using a featured image and images from the post content area in WordPress, we need to use an action hook called publish_{$post->post_type} ( triggered whenever a post, page or Custom Post Type(CPT) post is updated, or its new status is \"publish\" ).","_links":{"self":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts\/290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/comments?post=290"}],"version-history":[{"count":1,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts\/290\/revisions"}],"predecessor-version":[{"id":2306,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts\/290\/revisions\/2306"}],"wp:attachment":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/media?parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/categories?post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/tags?post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}