Veröffentlichung 28. August 2018 @ 23:44
Letzte Änderung 18. April 2021 @ 21:15
Überblick
- „Primitives“ Plugin zur Erstellung einer aktuellen XML-Sitemap-Datei für Beiträge und Seiten bei Veröffentlichung eines Beitrags / einer Seite
- Nutzung der PHP-XMLWriter-Erweiterung zur Erzeugung eines standarkonformen XML-Schemas für das Sitemapprotokoll
- Präfixe „self“ für PHP-Funktionen und „self-“ für Plugin-Verzeichnis und Plugin-Datei
Voraussetzungen
Netzwerk
- Website https://wordpress.example.com
Software
- WordPress 4.9.8 #https://wordpress.example.com/wp-admin/update-core.php
Ordner und Dateien
- Installation /usr/share/wordpress/wp-content/plugins/self-xml-sitemap
Plugin-Verzeichnis anlegen
mkdir /usr/share/wordpress/wp-content/plugins/self-xml-sitemap
Plugin-Datei erstellen
<?php /* Plugin Name: SELF XML Sitemap Description: Create updated XML sitemap file 'sitemap.xml' in main directory every time a page or post is published Version: 2018-08-28 Author: Thomas Fabian Author URI: https://www.fabiblog.de License: GPLv3 License URI: https://www.gnu.org/licenses/gpl-3.0.en.html */ if (!function_exists ('selfCreateXmlSitemap')) { function selfCreateXmlSitemap () { // Get all pages and posts $args = ['numberposts' => -1, 'post_type' => ['page', 'post']]; $posts = get_posts ($args); // Create XML sitemap file $xmlWriter = new XMLWriter (); $xmlFile = ABSPATH . 'sitemap.xml'; $xmlWriter->openURI ($xmlFile); $xmlWriter->setIndent (true); $xmlWriter->startDocument ('1.0', 'UTF-8'); $xmlWriter->startElement ('urlset'); $xmlWriter->writeAttribute ('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); foreach ($posts as $post) { setup_postdata ($post); $xmlWriter->startElement ('url'); $xmlWriter->writeElement ('loc', get_permalink ($post->ID)); $xmlWriter->writeElement ('lastmod', strstr ($post->post_modified, ' ', true)); $xmlWriter->endElement (); } $xmlWriter->endElement (); $xmlWriter->endDocument (); $xmlWriter->flush (); } // Hook function selfCreateXmlSitemap to actions publish_page and publish_post add_action ('publish_page', 'selfCreateXmlSitemap'); add_action ('publish_post', 'selfCreateXmlSitemap'); } ?>
Zugriffsrechte setzen
chown --recursive www-data:www-data /usr/share/wordpress/wp-content/plugins/self-xml-sitemap
Plugin aktivieren
https://wordpress.example.com/wp-admin/plugins.php SELF XML Sitemap - Aktivieren
Quellen
https://codex.wordpress.org/Writing_a_Plugin
https://developer.wordpress.org/plugins/
https://developer.wordpress.org/reference/
https://secure.php.net/manual/de/book.xmlwriter.php
https://www.sitemaps.org/protocol.html