Generating a sitemap.xml Sitemap with Pure PHP Code

4,076 0

A sitemap makes it easy for website administrators to tell search engines which web pages on their websites are available for crawling. The simplest form of a sitemap is the sitemap.xml file, which lists the URLs on a website along with other metadata about each URL (such as the time it was last updated, how frequently it changes, and its relative importance compared with other URLs on the website), so that search engines can crawl the website more intelligently.

For generating a sitemap.xml sitemap in WordPress, there are already dedicated plugins available in China, such as the well-known Baidu Sitemap Generator by Liucheng, and internationally, Google XML Sitemaps.

However, I feel that using a plugin solely to generate a sitemap.xml file is somewhat wasteful in terms of resources, so let's implement it with code instead! The code was found online. After making some modifications, I added support for generating XML for tags and categories, as well as the generation time for sitemap.xml.

PHP Code

Create a sitemap.php file and write the following code into it:

PHP
<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 1000;
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">';
?>
<!-- 生成时间 <?php echo $showtime = date("Y-m-d H:i:s"); ?> -->
<url>
    <loc><?php echo get_home_url(); ?></loc>
    <lastmod><?php $ltime = get_lastpostmodified(GMT); $ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
</url>
<?php
// 文章
$myposts = get_posts("numberposts=" . $posts_to_show);
foreach ($myposts as $post) { ?>
<url>
    <loc><?php the_permalink(); ?></loc>
    <lastmod><?php the_time('c'); ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
</url>
<?php } ?>
<?php
// 页面
$mypages = get_pages();
if (count($mypages) > 0) {
    foreach ($mypages as $page) { ?>
<url>
    <loc><?php echo get_page_link($page->ID); ?></loc>
    <lastmod><?php echo str_replace(" ", "T", get_page($page->ID)->post_modified); ?>+00:00</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.6</priority>
</url>
<?php }} ?>
<?php
// 分类
$terms = get_terms('category', 'orderby=name&hide_empty=0');
$count = count($terms);
if ($count > 0) {
    foreach ($terms as $term) { ?>
<url>
    <loc><?php echo get_term_link($term, $term->slug); ?></loc>
    <changefreq>weekly</changefreq>
    <priority>0.4</priority>
</url>
<?php }} ?>
<?php
// 标签
$tags = get_terms("post_tag");
foreach ($tags as $key => $tag) {
    $link = get_term_link(intval($tag->term_id), "post_tag");
    if (is_wp_error($link)) return false;
    $tags[$key]->link = $link;
?>
<url>
    <loc><?php echo $link; ?></loc>
    <changefreq>weekly</changefreq>
    <priority>0.2</priority>
</url>
<?php } ?>
</urlset>

Upload sitemap.php to the website's root directory, then access it to view the result.

Pseudo-Static Solution

Nginx

Edit the existing Nginx pseudo-static rules, add the following rule, and then restart Nginx:

Nginx
rewrite ^/sitemap.xml$ /sitemap.php last;

Apache

Edit the .htaccess file in the website's root directory and add the following rule:

Apache
RewriteRule ^(sitemap)\.xml$ $1.php

Fully Static Solution

Linux cron job + wget to periodically generate sitemap.xml (this solution is suitable for VPS users and not applicable to shared hosting)

Rename sitemap.php in the website's root directory to a PHP file name known only to you, such as xml.php.

Then use a Linux cron job to execute the following command, save the data as sitemap.xml, and place it in the website's root directory:

Bash
wget -O /home/www/www.ikxin.com/sitemap.xml https://www.ikxin.com/sitemap.php
  • https://www.ikxin.com/sitemap.php is the path to the PHP file.
  • /home/www/www.ikxin.com/sitemap.xml is the path where the XML file is stored.

This solves the problem of sitemap.xml containing dynamic data.

Comments

(0)

No comments yet. Start the conversation.

Leave a comment