2020-01-25 07:52:35 +01:00
|
|
|
<?php
|
|
|
|
// ----------------------
|
|
|
|
// 开一个api的统一URL
|
2020-01-27 15:28:34 +01:00
|
|
|
function gdk_sitemap_xml_flush_rules(){
|
2020-01-25 07:52:35 +01:00
|
|
|
$rules = get_option( 'rewrite_rules' );
|
|
|
|
if ( !isset( $rules['^sitemap(.*?)\.xml$'] ) ) {
|
|
|
|
global $wp_rewrite;
|
|
|
|
$wp_rewrite->flush_rules();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 添加自定义URL重写
|
2020-01-27 15:28:34 +01:00
|
|
|
function gdk_sitemap_xml_custom_rewrite_rule() {
|
2020-01-27 05:06:36 +01:00
|
|
|
add_rewrite_rule('^sitemap(.*?)\.xml$','index.php?sitemap=gdk$matches[1]','top');
|
2020-01-25 07:52:35 +01:00
|
|
|
}
|
|
|
|
|
2020-01-27 15:28:34 +01:00
|
|
|
function gdk_sitemap_xml_insert_query_vars( $vars ){
|
2020-01-25 07:52:35 +01:00
|
|
|
array_push($vars, 'sitemap');
|
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:28:34 +01:00
|
|
|
function gdk_sitemap_xml_cancel_redirect( $redirect_url ) {
|
2020-01-25 07:52:35 +01:00
|
|
|
$api_type = get_query_var('sitemap');
|
|
|
|
if ( !empty($api_type) ){
|
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
return $redirect_url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:28:34 +01:00
|
|
|
function gdk_sitemap_xml_api_handlers( $template ){
|
2020-01-25 07:52:35 +01:00
|
|
|
|
|
|
|
$hook = explode('-', get_query_var( 'sitemap' ) );
|
|
|
|
|
2020-01-27 05:06:36 +01:00
|
|
|
if( isset( $hook[0] ) && $hook[0] === 'gdk' ){
|
2020-01-25 07:52:35 +01:00
|
|
|
|
|
|
|
if( isset( $hook[1] ) ){
|
|
|
|
status_header(404);
|
|
|
|
header('HTTP/1.0 404 Not Found');
|
|
|
|
$GLOBALS['wp_query']->set_404();
|
|
|
|
include( get_query_template( '404' ) );
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2020-01-27 05:06:36 +01:00
|
|
|
$sitemap = get_transient('gdk-sitemap');
|
2020-01-25 07:52:35 +01:00
|
|
|
|
|
|
|
if( false === $sitemap || empty( $sitemap ) ){
|
2020-01-27 05:06:36 +01:00
|
|
|
$sitemap = gdk_create_sitemap();
|
|
|
|
set_transient( 'gdk-sitemap', $sitemap );
|
2020-01-25 07:52:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
header("Content-type: text/xml");
|
|
|
|
echo $sitemap;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:28:34 +01:00
|
|
|
add_action( 'init', 'gdk_sitemap_xml_custom_rewrite_rule' , 10, 0);
|
|
|
|
add_filter( 'query_vars', 'gdk_sitemap_xml_insert_query_vars' );
|
|
|
|
add_filter( 'redirect_canonical', 'gdk_sitemap_xml_cancel_redirect' );
|
|
|
|
add_action( 'wp_loaded', 'gdk_sitemap_xml_flush_rules' );
|
|
|
|
add_filter( 'template_include', 'gdk_sitemap_xml_api_handlers', 99 );
|
2020-01-25 07:52:35 +01:00
|
|
|
|
2020-01-27 05:06:36 +01:00
|
|
|
function gdk_create_sitemap() {
|
2020-01-25 07:52:35 +01:00
|
|
|
|
|
|
|
if ( str_replace( '-', '', get_option( 'gmt_offset' ) ) < 10 ) {
|
|
|
|
$tempo = '-0' . str_replace( '-', '', get_option( 'gmt_offset' ) );
|
|
|
|
} else {
|
|
|
|
$tempo = get_option( 'gmt_offset' );
|
|
|
|
}
|
|
|
|
if( strlen( $tempo ) == 3 ) { $tempo = $tempo . ':00'; }
|
|
|
|
$postsForSitemap = get_posts( array(
|
|
|
|
'numberposts' => -1,
|
|
|
|
'orderby' => 'modified',
|
|
|
|
'post_type' => array('post'),
|
|
|
|
'order' => 'DESC',
|
|
|
|
) );
|
|
|
|
$sitemap = '';
|
|
|
|
$sitemap .= '<?xml version="1.0" encoding="UTF-8"?>';
|
|
|
|
$sitemap .= "\n" . '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
|
|
|
|
$sitemap .= "\t" . '<url>' . "\n" .
|
|
|
|
"\t\t" . '<loc>' . esc_url( home_url( '/' ) ) . '</loc>' .
|
|
|
|
"\n\t\t" . '<lastmod>' . date( "Y-m-d\TH:i:s", current_time( 'timestamp', 0 ) ) . $tempo . '</lastmod>' .
|
|
|
|
"\n\t\t" . '<changefreq>daily</changefreq>' .
|
|
|
|
"\n\t\t" . '<priority>1.0</priority>' .
|
|
|
|
"\n\t" . '</url>' . "\n";
|
|
|
|
foreach( $postsForSitemap as $post ) {
|
|
|
|
setup_postdata( $post);
|
|
|
|
$postdate = explode( " ", $post->post_modified );
|
|
|
|
$sitemap .= "\t" . '<url>' . "\n" .
|
|
|
|
"\t\t" . '<loc>' . get_permalink( $post->ID ) . '</loc>' .
|
|
|
|
"\n\t\t" . '<lastmod>' . $postdate[0] . 'T' . $postdate[1] . $tempo . '</lastmod>' .
|
|
|
|
"\n\t\t" . '<changefreq>Weekly</changefreq>' .
|
|
|
|
"\n\t\t" . '<priority>0.5</priority>' .
|
|
|
|
"\n\t" . '</url>' . "\n";
|
|
|
|
}
|
|
|
|
$sitemap .= '</urlset>';
|
|
|
|
return $sitemap;
|
|
|
|
}
|
|
|
|
|
2020-01-27 05:06:36 +01:00
|
|
|
function gdk_clear_sitemap_cache(){
|
|
|
|
delete_transient( 'gdk-sitemap' );
|
2020-01-25 07:52:35 +01:00
|
|
|
}
|
2020-01-27 05:06:36 +01:00
|
|
|
add_action("publish_post", "gdk_clear_sitemap_cache");
|
|
|
|
add_action("publish_page", "gdk_clear_sitemap_cache");
|
|
|
|
add_action( "save_post", "gdk_clear_sitemap_cache" );
|