Last active
September 20, 2024 14:20
-
-
Save cuxaro/28c3107df6ae1b0f5a4bc63253717f4f to your computer and use it in GitHub Desktop.
Personalizar el Sitemap de YOAST mediante Filter hooks
This file contains hidden or 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 | |
/** | |
* Hook para agregar sitemaps personalizados al sitemap_index.xml de Yoast. | |
* | |
* @param string $sitemap_index El contenido del índice del sitemap de Yoast. | |
* @return string El índice del sitemap modificado. | |
*/ | |
function ibp_agregar_sitemaps_personalizados( $sitemap_index ) { | |
// Obtener las URLs de los sitemaps personalizados a través de un filtro. | |
$sitemap_urls = apply_filters( 'ibp_sitemaps_urls', array() ); | |
// Verificar que haya URLs en el array. | |
if ( ! empty( $sitemap_urls ) && is_array( $sitemap_urls ) ) { | |
foreach ( $sitemap_urls as $sitemap_url ) { | |
// Agregar cada sitemap al índice, asegurándonos de que la URL sea válida. | |
$sitemap_index .= '<sitemap><loc>' . esc_url( $sitemap_url ) . '</loc></sitemap>'; | |
} | |
} | |
return $sitemap_index; | |
} | |
add_filter( 'wpseo_sitemap_index', 'ibp_agregar_sitemaps_personalizados' ); | |
// Añadir URLs de sitemaps personalizados al índice. | |
add_filter( 'ibp_sitemaps_urls', function( $sitemaps ) { | |
// Añadir múltiples URLs al array. | |
$sitemaps[] = home_url('/antiguas_sitemap.xml'); | |
$sitemaps[] = home_url('/actual_antiguas.xml'); | |
return $sitemaps; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment