Last active
February 22, 2018 01:56
-
-
Save jkereako/6081728 to your computer and use it in GitHub Desktop.
Add your categories to WordPress.com's sitemap.xml.
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 | |
/** | |
* Adds categories to the sitemap by filtering wpcom_print_sitemap. | |
* | |
* @uses array_key_exists, esc_url, function_exists, get_categories, is_array, taxonomy_image_plugin_get_associations, taxonomy_image_plugin_get_image_src, trailingslashit, wpcom_sitemap_array_to_simplexml | |
* @return array | |
*/ | |
function nesn_filter_wpcom_print_sitemap( $tree, $latest_mod ) { | |
$url_base = trailingslashit( get_bloginfo( 'siteurl' ) ); | |
$categories = array(); | |
$category_images = null; | |
$url = null; | |
// Sanity check. This function will only be available if the | |
// taxonomy image plugin is enabled. | |
if ( function_exists( 'taxonomy_image_plugin_get_associations' ) ) { | |
$category_images = &taxonomy_image_plugin_get_associations(); | |
} | |
// Iterate over each category | |
foreach ( get_categories() as $category ) { | |
$url = array( | |
'loc' => esc_url( $url_base . $category->slug ), | |
'changefreq' => 'monthly', | |
'image' => null | |
); | |
// More sanity checks | |
if ( is_array( $category_images ) && function_exists( 'taxonomy_image_plugin_get_image_src' ) ) { | |
// Does the category have an image associated with it? | |
if ( array_key_exists( $category->term_id, $category_images ) ) { | |
$url['image']['loc'] = esc_url( taxonomy_image_plugin_get_image_src( $category_images[ $category->term_id ] ) ); | |
} | |
} | |
// Wrap the entire URL object in an array so that it is properly | |
// formatted when the XML is printed to stdout. | |
wpcom_sitemap_array_to_simplexml( array( 'url' => $url ), $tree ); | |
} | |
return $tree; | |
} | |
add_filter( 'wpcom_print_sitemap', 'nesn_filter_wpcom_print_sitemap', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment