Last active
March 28, 2024 05:19
-
-
Save leepowers/2b32f734571cbeadc811b93501cfcced to your computer and use it in GitHub Desktop.
WordPress Yoast SEO: Create a custom sitemap with data not sourced from a custom post type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* USAGE: | |
* - Search and replace the `CUSTOM_SITEMAP` string with a lowercase identifier name: e.g., "vehicles", "customer_profiles" | |
* - The `your_alternate_data_source()` function does not exist; it's simply a placeholder for your own function that returns some sort of data array. | |
* - Uses heredocs for inline XML: https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc | |
*/ | |
/** | |
* Uncomment the next line of code to disable sitemap caching. | |
* - For development environments and debugging only. | |
* - All other scenarios, follow the "Manual Sitemap Update" instructions in the Yoast documentation: | |
* - https://yoast.com/help/sitemap-does-not-update/#manual | |
*/ | |
#add_filter("wpseo_enable_xml_sitemap_transient_caching", "__return_false"); | |
/** | |
* Add CUSTOM_SITEMAP-sitemap.xml to Yoast sitemap index | |
*/ | |
function CUSTOM_SITEMAP_sitemap_index($sitemap_index) { | |
global $wpseo_sitemaps; | |
$sitemap_url = home_url("CUSTOM_SITEMAP-sitemap.xml"); | |
$sitemap_date = date(DATE_W3C); # Current date and time in sitemap format. | |
$custom_sitemap = <<<SITEMAP_INDEX_ENTRY | |
<sitemap> | |
<loc>%s</loc> | |
<lastmod>%s</lastmod> | |
</sitemap> | |
SITEMAP_INDEX_ENTRY; | |
$sitemap_index .= sprintf($custom_sitemap, $sitemap_url, $sitemap_date); | |
return $sitemap_index; | |
} | |
add_filter("wpseo_sitemap_index", "CUSTOM_SITEMAP_sitemap_index"); | |
/** | |
* Register CUSTOM_SITEMAP sitemap with Yoast | |
*/ | |
function CUSTOM_SITEMAP_sitemap_register() { | |
global $wpseo_sitemaps; | |
if (isset($wpseo_sitemaps) && !empty($wpseo_sitemaps)) { | |
$wpseo_sitemaps->register_sitemap("CUSTOM_SITEMAP", "CUSTOM_SITEMAP_sitemap_generate"); | |
} | |
} | |
add_action("init", "CUSTOM_SITEMAP_sitemap_register"); | |
/** | |
* Generate CUSTOM_SITEMAP sitemap XML body | |
*/ | |
function CUSTOM_SITEMAP_sitemap_generate() { | |
global $wpseo_sitemaps; | |
$data = your_alternate_data_source(); # Replace this with your own data source function | |
$urls = array(); | |
foreach ($data as $item) { | |
$urls[]= $wpseo_sitemaps->renderer->sitemap_url(array( | |
"mod" => $item->date, # <lastmod></lastmod> | |
"loc" => $item->url, # <loc></loc> | |
"images" => array( | |
array( # <image:image></image:image> | |
"src" => $item->image_url, # <image:loc></image:loc> | |
"title" => $item->image_title, # <image:title></image:title> | |
"alt" => $item->image_title, # <image:caption></image:caption> | |
), | |
), | |
)); | |
} | |
$sitemap_body = <<<SITEMAP_BODY | |
<urlset | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" | |
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" | |
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | |
%s | |
</urlset> | |
SITEMAP_BODY; | |
$sitemap = sprintf($sitemap_body, implode("\n", $urls)); | |
$wpseo_sitemaps->set_sitemap($sitemap); | |
} |
@anuranjanverma
So what you do to achieve this is to create a Chunk of an array with all the URLs that you want to add to the SITEMAP and use this Query Variable get_query_var('sitemap_n')
which is provided by Yoast for the sitemap paginations and based on the Pagination you can add the URLs to the sitemap page. I used this to solve this and it is working absolutely fine.
Hi how to add double image ? E.g product variants have photo ids like 11111,22222,33333 in the 'product_image_galery' meta. How can I add?
If you're talking about Product Gallery Images, then you can achieve this by:
$attach_ids= "11111,22222,33333";
update_post_meta( $product_id, '_product_image_gallery', $attach_ids);
NOTE: The $product_id would be your parent product id.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@anuranjanverma You can use your own sitemap provider that implements
WPSEO_Sitemap_Provider
and hook it to thewpseo_sitemaps_providers
filter. Not battle tested yet, but it seems to do the job.Here's an example of implementation: https://gist.github.com/7ute/0ae53b65db64e2752a516e9186a1c744