Last active
November 7, 2024 18:34
-
-
Save amboutwe/8cfb7a3d8f05e580867341d4ff84141d to your computer and use it in GitHub Desktop.
Filters and example code for Yoast SEO sitemaps
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 | |
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/ | |
/* Create a Yoast SEO sitemap for a custom post type | |
* Credit: Team Yoast | |
* Last Tested: Unknown | |
* Documentation: https://developer.yoast.com/features/xml-sitemaps/api/#add-a-custom-post-type | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
* HOW TO USE | |
* Replace TYPE with your custom type | |
*/ | |
add_action('init', 'enable_custom_sitemap'); | |
function enable_custom_sitemap() | |
{ | |
global $wpseo_sitemaps; | |
if (isset($wpseo_sitemaps) && !empty($wpseo_sitemaps)) { | |
$wpseo_sitemaps->register_sitemap('TYPE', 'create_TYPE_sitemap'); | |
} | |
} |
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 | |
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/ | |
/* Add External Sitemap to Yoast Sitemap Index | |
* Credit: Paul https://wordpress.org/support/users/paulmighty/ | |
* Modified by: Team Yoast | |
* Last Tested: Aug 25 2017 using Yoast SEO 5.3.2 on WordPress 4.8.1 | |
* Documentation: | |
* https://developer.yoast.com/features/xml-sitemaps/api/#add-additionalexternal-xml-sitemaps-to-the-xml-sitemap-index | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
* This code adds two external sitemaps and must be modified before using. | |
* Replace http://www.example.com/external-sitemap-#.xml with your external sitemap URL. | |
* Replace 2017-05-22T23:12:27+00:00 with the time and date your external sitemap was last updated. | |
* Format: yyyy-MM-dd'T'HH:mm:ssZ | |
* If you have more/less sitemaps, add/remove the additional section. | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
* Please note that changes will be applied upon next sitemap update. | |
* To manually refresh the sitemap, please disable and enable the sitemaps. | |
*/ | |
add_filter('wpseo_sitemap_index', 'add_sitemap_custom_items'); | |
function add_sitemap_custom_items($sitemap_custom_items) | |
{ | |
$sitemap_custom_items .= ' | |
<sitemap> | |
<loc>http://www.example.com/external-sitemap-1.xml</loc> | |
<lastmod>2017-05-22T23:12:27+00:00</lastmod> | |
</sitemap>'; | |
/* Add Additional Sitemap | |
* This section can be removed or reused as needed | |
*/ | |
$sitemap_custom_items .= ' | |
<sitemap> | |
<loc>http://www.example.com/external-sitemap-2.xml</loc> | |
<lastmod>2017-05-22T23:12:27+00:00</lastmod> | |
</sitemap>'; | |
/* DO NOT REMOVE ANYTHING BELOW THIS LINE | |
* Send the information to Yoast SEO | |
*/ | |
return $sitemap_custom_items; | |
} |
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 | |
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/ | |
/* Add single image to all content items in the Yoast SEO sitemap | |
* Credit: Yoast team | |
* Documentation: https://developer.yoast.com/features/xml-sitemaps/api/ | |
* Last Tested: Aug 08 2024 using Yoast SEO 23.2 on WordPress 6.6.1 | |
*/ | |
function filter_wpseo_sitemap_urlimages( $images ) { | |
array_push( $images, [ 'src' => 'https://www.example.com/wp-content/uploads/extra-image.jpg' ]); | |
return $images; | |
}; | |
add_filter( 'wpseo_sitemap_urlimages', 'filter_wpseo_sitemap_urlimages' ); // Post Types (posts, pages, custom post) | |
add_filter( 'wpseo_sitemap_urlimages_term', 'filter_wpseo_sitemap_urlimages' ); // Taxonomies (category, tags) |
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 | |
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/ | |
/* Change the number of entries per sitemap | |
* Credit: Yoast Team | |
* Last Tested: Oct 17 2018 using Yoast SEO 8.4 on WordPress 4.9.8 | |
* Limit should be either lower than 100 or a multiple of 100 to avoid problems with the sitemap pagination | |
* Documentation: https://developer.yoast.com/features/xml-sitemaps/api/#alter-the-number-of-sitemap-entries | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
*/ | |
add_filter('wpseo_sitemap_entries_per_page', 'max_entries_per_sitemap'); | |
function max_entries_per_sitemap() { | |
return 10; | |
} |
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 | |
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/ | |
/* Remove homepage from the page sitemap | |
* Credit: Yoast team | |
* Last Tested: Jun 20 2024 using Yoast SEO 22.9 on WordPress 6.5.4 | |
*/ | |
add_filter( 'wpseo_sitemap_url', function ( $output, $url ) { | |
if ( $url['loc'] === home_url( '/' ) ) { | |
return ''; | |
} | |
return $output; | |
}, 10, 2 ); |
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 | |
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/ | |
/* Remove all image URLs for the Yoast SEO sitemap | |
* Credit: Yoast team | |
* Last Tested: Aug 08 2024 using Yoast SEO 23.2 on WordPress 6.6.1 | |
*/ | |
// Post Types (posts, pages, custom post types) | |
add_filter( 'wpseo_sitemap_urlimages', '__return_empty_array' ); | |
// Taxonomies (category, tags) | |
add_filter( 'wpseo_sitemap_urlimages_term', '__return_empty_array' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@djboris88 The plugin automatically detects most custom post types (CPT), so you'd only need this in some instances. You can check under Admin > Yoast SEO > Settings > Content types to see if your CPT was detected automatically.
The code requires that you replace
TYPE
with your CPT. For example, if your CPT is calledrecipe
, the register sitemap line would change as shown here.$wpseo_sitemaps->register_sitemap( 'recipe', 'create_recipe_sitemap' );