Blog sitemap

WordPress

Expose /blog.xml from WordPress with a small rewrite and XML proxy callback.

Setup steps

  1. 1Add the snippet to a site-specific plugin or your theme functions.php.
  2. 2Replace YOUR_SITE_ID with the Rootscript site id shown in the popup URL.
  3. 3Flush permalinks once by visiting Settings > Permalinks and saving.
  4. 4Open /blog.xml in the browser, then click Check solution in Rootscript.

WordPress snippet

Replace YOUR_SITE_ID with the site id from the Rootscript sitemap URL shown in your dashboard popup.

add_action('init', function () {
  add_rewrite_rule('^blog\.xml$', 'index.php?rootscript_blog_xml=1', 'top');
});

add_filter('query_vars', function ($vars) {
  $vars[] = 'rootscript_blog_xml';
  return $vars;
});

add_action('template_redirect', function () {
  if (!get_query_var('rootscript_blog_xml')) {
    return;
  }

  $response = wp_remote_get('https://rootscript.io/api/site/YOUR_SITE_ID/sitemap.xml');

  if (is_wp_error($response)) {
    status_header(502);
    exit('Sitemap unavailable');
  }

  header('Content-Type: application/xml; charset=utf-8');
  header('Cache-Control: no-store, max-age=0');
  echo wp_remote_retrieve_body($response);
  exit;
});