DAY_IN_SECONDS ) { fictioneer_create_sitemap(); } // Serve the sitemap file header( 'Content-Type: application/xml' ); readfile( $sitemap_file ); exit; } add_action( 'template_redirect', 'fictioneer_serve_sitemap' ); // ============================================================================= // (RE-)BUILD SITEMAP // ============================================================================= /** * Get tag helper * * @since 4.0.0 * * @param string $content Content for the tag. * * @return string Tag with content. */ function fictioneer_loc_node( $content ) { return '' . $content . ''; } /** * Get tag helper * * @since 4.0.0 * * @param string $content Content for the tag. * * @return string Tag with content. */ function fictioneer_lastmod_node( $content ) { return '' . $content . ''; } /** * Get tag helper * * @since 4.0.0 * * @param string $content Content for the tag. * * @return string Tag with content. */ function fictioneer_frequency_node( $content ) { return '' . $content . ''; } /** * Get node helper * * @since 4.0.0 * * @param string $loc URL. * @param string $lastmod Optional. Last modified timestamp. * @param string $freq Optional. Refresh frequency, e.g. 'daily' or 'yearly'. * * @return string Node with tags. */ function fictioneer_url_node( $loc, $lastmod = null, $freq = null ) { $node = "\t" . '' . "\n\t\t" . fictioneer_loc_node( $loc ) . "\n"; $node = $lastmod ? $node . "\t\t" . fictioneer_lastmod_node( $lastmod ) . "\n" : $node; $node = $freq ? $node . "\t\t" . fictioneer_frequency_node( $freq ) . "\n" : $node; $node .= "\t" . '' . "\n"; return $node; } /** * Generate theme sitemap * * @since 4.0.0 * @since 5.8.7 - Create on demand, not on post save. */ function fictioneer_create_sitemap() { // Open $sitemap = '' . "\n"; $sitemap .= '' . "\n"; // Blog and front page $sitemap .= fictioneer_url_node( esc_url( home_url( '/' ) ), current_time( 'c' ), 'daily' ); $sitemap .= fictioneer_url_node( get_permalink( get_option( 'page_for_posts' ) ), current_time( 'c' ), 'daily' ); // Pages $pages = get_posts( array( 'post_type' => 'page', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', 'numberposts' => '1000', 'post__not_in' => [get_option( 'page_on_front' ), get_option( 'page_for_posts' )], 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'no_found_rows' => true ) ); foreach ( $pages as $post ) { $post_id = $post->ID; $template = get_page_template_slug( $post_id ); $template_excludes = ['user-profile.php', 'singular-bookmarks.php', 'singular-bookshelf.php', 'singular-bookshelf-ajax.php']; $template_excludes = apply_filters( 'fictioneer_filter_sitemap_page_template_excludes', $template_excludes ); if ( in_array( $template, $template_excludes ) ) { continue; } $lastmod = get_the_modified_date( 'c', $post_id ); $sitemap .= fictioneer_url_node( get_permalink( $post_id ), $lastmod, 'monthly' ); } // Blogs $blogs = get_posts( array( 'post_type' => 'post', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', 'numberposts' => '1000', 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'no_found_rows' => true ) ); foreach ( $blogs as $post ) { $lastmod = get_the_modified_date( 'c', $post->ID ); $sitemap .= fictioneer_url_node( get_permalink( $post->ID ), $lastmod, 'never' ); } // Collections $collections = get_posts( array( 'post_type' => 'fcn_collection', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', 'numberposts' => '1000', 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'no_found_rows' => true ) ); foreach ( $collections as $post ) { $lastmod = get_the_modified_date( 'c', $post->ID ); $sitemap .= fictioneer_url_node( get_permalink( $post->ID ), $lastmod, 'monthly' ); } // Stories $stories = get_posts( array( 'post_type' => 'fcn_story', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', 'numberposts' => '2000', 'update_post_meta_cache' => true, 'update_post_term_cache' => false, 'no_found_rows' => true ) ); foreach ( $stories as $post ) { $post_id = $post->ID; if ( get_post_meta( $post_id, 'fictioneer_story_hidden', true ) ) { continue; } $lastmod = get_the_modified_date( 'c', $post_id ); $status = get_post_meta( $post_id, 'fictioneer_story_status', true ); $frequency = $status == 'Ongoing' ? 'weekly' : 'monthly'; $sitemap .= fictioneer_url_node( get_permalink( $post_id ), $lastmod, $frequency ); } // Chapters $chapters = get_posts( array( 'post_type' => 'fcn_chapter', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', 'numberposts' => '10000', 'update_post_meta_cache' => true, 'update_post_term_cache' => false, 'no_found_rows' => true ) ); foreach ( $chapters as $post ) { if ( get_post_meta( $post->ID, 'fictioneer_chapter_hidden', true ) ) { continue; } $lastmod = get_the_modified_date( 'c', $post->ID ); $sitemap .= fictioneer_url_node( get_permalink( $post->ID ), $lastmod, 'monthly' ); } // Recommendations $recommendations = get_posts( array( 'post_type' => 'fcn_recommendation', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', 'numberposts' => '1000', 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'no_found_rows' => true ) ); foreach ( $recommendations as $post ) { $lastmod = get_the_modified_date( 'c', $post->ID ); $sitemap .= fictioneer_url_node( get_permalink( $post->ID ), $lastmod, 'monthly' ); } // End $sitemap .= "\n" . ''; // Save $file_path = fopen( ABSPATH . '/fictioneer_sitemap.xml', 'w' ); fwrite( $file_path, $sitemap ); fclose( $file_path ); }