• Resolved becahp

    (@becahp)


    Hi! I have been using the Slim SEO plugin for my website, and it has been an invaluable tool in optimizing our site’s SEO. I appreciate the great work you’ve done with this plugin!

    I am currently working on a project where I need to include some custom URLs in the Slim SEO sitemap. These custom URLs are dynamic content loaded from an external API service. While Slim SEO is already successfully generating the primary pages on my website, I need to map these dynamic URLs to the sitemap as well.

    For example, I have a landing page at mywebsite.com/cientista, which serves as a hub for various scientists. The actual content for each scientist is dynamically loaded from the API and is available at URLs like mywebsite.com/cientista/?item_id=xxx. I have the ability to generate these custom URLs programmatically from the API, but I’m struggling to get them to appear in the Slim SEO sitemap.

    I’ve attempted to use the slim_seo_sitemap_post hook to add these custom URLs to the sitemap, but I haven’t been successful. Here’s a simplified version of my code:

    add_filter('slim_seo_sitemap_post', 'output_test');
    function output_test() {
        echo "\t<url>\n";
        echo "\t\t<loc>mywebsite.com/cientista/?item_id=1</loc>\n";
        echo "\t</url>\n";
    }

    Similarly, I’ve tried to use this code in my custom plugin with no actual results:

    add_action('slim_seo_sitemap_post', [$this, 'output_test']);

    I’m reaching out to you for guidance on how I can effectively add these custom URLs to the Slim SEO sitemap. Your expertise and assistance in this matter would be greatly appreciated.

    If you have any suggestions, examples, or guidance on how to achieve this, I would be grateful for your assistance. Thank you in advance for your time and support.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Anh Tran

    (@rilwis)

    Hi @becahp ,

    The hook slim_seo_sitemap_post you used is correct. However, this hook fires for each post in the sitemap, so the content output in this hook is actually insite the current <url></url> block. See the plugin source code to understand.

    To add more URLs, you need to close the current <url> block and add your new ones, like this:

    add_filter('slim_seo_sitemap_post', 'output_test');
    function output_test() {
        echo "\t</url>\n";
        echo "\t<url>\n";
        echo "\t\t<loc>mywebsite.com/cientista/?item_id=1</loc>\n";
        echo "\t</url>\n";
    }
    Thread Starter becahp

    (@becahp)

    Hi @rilwis , thank you so much for your prompt and helpful response. With your directions, I was able to add a custom URL to SlimSEO sitemap successfully.

    I just want to share a couple of notes based on your instructions:

    Indeed we need to close the current <url> but we don’t need that last echo to close the new URL we’re adding. In the end, the code looks like this:

    add_filter('slim_seo_sitemap_post', 'output_test');
    function output_test() {
        echo "\t</url>\n";
        echo "\t<url>\n";
        echo "\t\t<loc>mywebsite.com/cientista/?item_id=1</loc>\n";
    }

    Lastly. for anyone else attempting to add custom URLs using the code provided, please keep in mind that it will add the URL to every post that the plugin’s crawler goes through when building the sitemap. To prevent this, I’ll be adding some conditions using the global $post variable in the function that the hook accepts. This way, we can make sure the URL is added just once.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How to add custom URLs to the sitemap?’ is closed to new replies.