IT Blog

  • Blog
  • Technology
    • Technology
    • Architecture
    • CMS
    • CRM
    • Web
    • DotNET
    • Python
    • Database
    • BI
    • Program Language
  • Users
    • Login
    • Register
    • Forgot Password?
  • ENEN
    • 中文中文
    • ENEN
Experience IT
In a World of Technology, People Make the Difference.
  1. Home
  2. Technology
  3. CMS
  4. Wordpress
  5. Plugin
  6. Generating XML SiteMap

Generating XML SiteMap

2019-04-11 931 Views 0 Like 0 Comments

Search engine robots require sitemap in pure XML format, without any additional content, header, or footer. But WordPress plugin is just for content, when the shortcode being used in pages or posts it will be added with header, menu, and footer automatically.

In order to generate pure XML we need to control the output. Using ob_ (output buffer) functions we can achieve this goal, exit() function ignores all following outputs.

1. Add ob_start() to top of header.php file under child theme root folder.

2. Add plugin function to generate sitemap in xml format.

function sitemap_post_shortcode($atts) {
    global $wpdb;

    $baseurl = 'https://211cn.ca';
    $sql = "SELECT concat('$baseurl/',year(post_date),'/',month(post_date),'/',post_name) url ,post_modified from wp_posts WHERE post_type='post' and post_status='publish' order by post_modified DESC";
    $result = $wpdb->get_results($sql);

    $doc = '<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="'. $baseurl .'/wp-content/plugins/albert/sitemap.xsl"?>
<!-- generated-on="'. date("Y-m-d\TH:i:s+00:00") .'" -->
';
    $urlset = $doc.'<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">
';

    foreach($result as $row) {
    $urlset .= '<url>
        <loc>'.$row->url.'</loc>
        <lastmod>'.date('Y-m-d\TH:i:s+00:00',strtotime($row->post_modified)).'</lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.3</priority>
    </url>';
    }
    $urlset.="
</urlset>";

    ob_clean();
    echo $urlset;
    ob_flush();
    exit();
}
add_shortcode('211postmap', 'sitemap_post_shortcode');

Note that the date in <lastmod> element is required in this format: <lastmod>2019-04-10T11:22:33+00:00</lastmod>, using this code to convert:

date('Y-m-d\TH:i:s+00:00',strtotime($row-&gt;post_modified))

Final result

site map in xml format

Loading

error
fb-share-icon
Tweet
fb-share-icon
IT Team
Author: IT Team

Tags: None
Last updated:2019-04-11

IT Team

This person is lazy and left nothing

Like
< Previous
Next >

Comments

Cancel reply
Newest Hotspots Random
Newest Hotspots Random
Rich editor not working Making web page scroll down automatically Getting data from Dapper result All Unicode Chars How to keep and display contact form 7 data Common Regular Expressions
Setting Up Mail for [Contact Form 7] Query menu in WordPress Feature points sampling method validating reports Geting twitter streaming contents in WordPress OneDrive for Business 文件到 Google Drive 文件 Useful sql script for SQL Server developers
Categories
  • Architecture
  • BI
  • C#
  • CSS
  • Database
  • DotNET
  • Hosting
  • HTML
  • JavaScript
  • PHP
  • Program Language
  • Python
  • Security
  • SEO
  • Technology
  • Web
  • Wordpress

COPYRIGHT © 2021 Hostlike IT Blog. All rights reserved.

This site is supported by Hostlike.com