S
S
Sergey Burduzha2021-09-06 15:46:39
Create a sitemap
Sergey Burduzha, 2021-09-06 15:46:39

How to dynamically add pages to wp sitemap?

Good afternoon.
I am using the rank math plugin for seo.

The site displays posts that are created dynamically.
I created an ads page, and I get the content of the post through the api.
Example via fetch.

fetch('https://my.site/api/realestate/v1/immobili/<?php echo $_GET['id']; ?>');


And I display content on the page.

Now, how do I add these pages to the sitemap?
I can get all the id records and create a url in a loop.

I just don’t understand how to register in the sitemap for rankmath.

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Burduzha, 2021-10-21
@serii81

Found a solution.

add_action("init", "eg_create_sitemap");

$sitemap_path = ABSPATH . "sitemap.xml";
$pages_path = ABSPATH . "pages.xml";
$ads_path = ABSPATH . "ads-list.xml";

if (file_exists($sitemap_path)) {
  unlink($sitemap_path);
}
if (file_exists($pages_path)) {
  unlink($pages_path);
}
if (file_exists($ads_path)) {
  unlink($ads_path);
}

function createUrlXml() {
  $url_sitemap = '<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="' . get_template_directory_uri() . '/helpers/sitemap.css"?>';
  $url_sitemap .= "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
  $url_sitemap .= "\t" . '<url>' . "\n" . "\t\t" . '<loc>https://www.imprendocasa.it/ads-list.xml</loc>' . "\n\t\t" . '<lastmod>2021-09-30T01:10:02.000000Z</lastmod>' . "\n\t\t" . '<changefreq>monthly</changefreq>' . "\n\t" . '
    </url>' . "\n";
  $url_sitemap .= "\t" . '<url>' . "\n" . "\t\t" . '<loc>https://www.imprendocasa.it/pages.xml</loc>' . "\n\t\t" . '
        <lastmod>2021-09-30T01:10:02.000000Z</lastmod>' . "\n\t\t" . '<changefreq>monthly</changefreq>' . "\n\t" . '
    </url>' . "\n";
  $url_sitemap .= '</urlset>';
  return $url_sitemap;
}

function createPagesSitemap() {
  $postsForSitemap = get_posts(array('numberposts' => -1, 'orderby' => 'modified', 'post_type' => array('post', 'page'), 'order' => 'DESC'));
  $pages_sitemap = '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/css" href="' . get_template_directory_uri() . '/helpers/sitemap.css"?>';
  $pages_sitemap .= "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
  foreach ($postsForSitemap as $post) {
    setup_postdata($post);
    $postdate = explode(" ", $post->post_modified);
    $pages_sitemap .= "\t" . '<url>' . "\n" . "\t\t" . '<loc>' . get_permalink($post->ID) . '</loc>' . "\n\t\t" . '
        <lastmod>' . $postdate[0] . '</lastmod>' . "\n\t\t" . '<changefreq>monthly</changefreq>' . "\n\t" . '</url>' . "\n";
  }
  $pages_sitemap .= '</urlset>';
  return $pages_sitemap;
}

function createAdsSitemap() {
  $adsForSitemap = getPropertiesId();
  $adsForSitemap = json_decode($adsForSitemap)->data;
  $ads_sitemap = '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/css" href="' . get_template_directory_uri() . '/helpers/sitemap.css"?>';
  $ads_sitemap .= "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
  foreach ($adsForSitemap as $item) {
    $ads_sitemap .= "\t" . '<url>' . "\n" . "\t\t" . '<loc>https://www.imprendocasa.it/ads?id=' . $item->id . '</loc>' . "\n\t\t" . '<lastmod>' . $item->updated_at . '</lastmod>' . "\n\t\t" . '<changefreq>monthly</changefreq>' . "\n\t" . '</url>' . "\n";
  }
  $ads_sitemap .= '</urlset>';
  return $ads_sitemap;
}

function eg_create_sitemap() {
  $url_sitemap = createUrlXml();
  $pages_sitemap = createPagesSitemap();
  $ads_sitemap = createAdsSitemap();

  global $sitemap_path;
  global $pages_path;
  global $ads_path;

  $fp = fopen($sitemap_path, 'w');
  fwrite($fp, $url_sitemap);
  fclose($fp);

  $fp = fopen($pages_path, 'w');
  fwrite($fp, $pages_sitemap);
  fclose($fp);

  $fp = fopen($ads_path, 'w');
  fwrite($fp, $ads_sitemap);
  fclose($fp);
}

$adsForSitemap - get data from api.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question