Add External URL in Sitemap Rankmath hook

#633831
  • Resolved Aayush Bansal
    Rank Math free

    I am trying to add one more external URL on sitemap using hook but seems nothing happening

    function add_external_url_to_sitemap($urls) {
    // Define your external URL here
    $external_url = ‘https://example.com/external-page’;

    // Add the external URL to the sitemap array
    $urls[] = array(
    ‘loc’ => $external_url,
    ‘lastmod’ => date(‘c’), // Use the current date as the last modification date
    ‘changefreq’ => ‘weekly’, // You can customize this
    ‘priority’ => 0.5, // You can customize this
    );

    return $urls;
    }
    add_filter(‘rank_math/sitemap/urls’, ‘add_external_url_to_sitemap’);

    here is my code. i also used rank_math/sitemap/index hoooks but nothing working.

    How i can implement this

Viewing 10 replies - 1 through 10 (of 10 total)
  • http://localhost/test/sitemap_index.xml (this is default rankmath sitemap). i want to add additional url in that

    Hello,

    Thank you for contacting us and sorry for any inconvenience that might have been caused due to that.

    Can you please confirm if you want to add some additional URLs to your existing sub-sitemaps? If so, then you can try to use the following filter on your website:

    function add_external_sitemaps($sitemap) {
        // Add your external sitemap URLs here
        $external_sitemaps = array(
            'https://example.com/external-sitemap1.com',
            'https://example.com/external-sitemap2.com',
        );
    
        foreach ($external_sitemaps as $external_sitemap) {
            $sitemap .= '<url><loc>'.esc_url($external_sitemap).'</loc>
    		<lastmod>2020-06-10T20:20:20+00:00</lastmod>
    		</url>';
        }
    
        return $sitemap;
    }
    
    add_action( 'rank_math/sitemap/{type}_content', 'add_external_sitemaps');
    

    Please make sure to change the {type} where you want to add the additional URLs. If you want to add those URLs to your post sitemap then change it like this: rank_math/sitemap/post_content

    In case, you already have some sitemap files and want to include them in your main /sitemap_index.xml then you can use the following filter:

    add_filter( 'rank_math/sitemap/index', function( $xml ) {
    	$xml .= '
    		<sitemap>
    			<loc>https://example.com/external-sitemap1.xml</loc>
    			<lastmod>2020-09-14T20:34:15+00:00</lastmod>
    		</sitemap>';
    		return $xml;
    }, 11 );
    

    Here’s how you can add filter/hook to your WordPress site: https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Hope that helps and please don’t hesitate to let us know if you have any other questions.
     
    Thank you.

    it worked but there is one issue. i want to check if plugin is installed or not. Sometimes i am not sure if we have insalled the RanKMATH or not

    add_filter(‘rank_math/sitemap/index’, ‘custom_rank_math_sitemap_index’, 11);
    add_filter(‘rank_math/sitemap/enable_caching’, ‘disable_rank_math_sitemap_caching’);

    Above code is working very well.
    if i use
    if (class_exists(‘RankMath’)) {
    add_filter(‘rank_math/sitemap/index’, ‘custom_rank_math_sitemap_index’, 11);
    add_filter(‘rank_math/sitemap/enable_caching’, ‘disable_rank_math_sitemap_caching’);

    }

    this is not working.

    Hello,

    If the filter code is working, then Rank Math is installed. However, the class should still be detected. Can you please clear your website cache and try checking the sitemap again?

    However, you don’t need to check for its class because our filter code will not generate an issue once the plugin is deactivated even without a condition statement to check for the class.

    Looking forward to helping you.

    We still need to check the class exist or not.

    Hello,

    I have tested this on our local WP setup and checking the class seems to be working as intended as you can see in my video screencast here: https://i.rankmath.com/v/aw9r5t

    If the issue persists on your end, please make sure that you are adding the code to the correct file. Mind if I ask where you have added the code? Are you perhaps using a Code Snippet plugin? Let us know.

    Looking forward to helping you.

    Your code is not working if we add
    if (class_exists(‘RankMath’)) {
    add_filter(‘rank_math/sitemap/index’, ‘custom_rank_math_sitemap_index’, 11);
    add_filter(‘rank_math/sitemap/enable_caching’, ‘disable_rank_math_sitemap_caching’);

    }

    but if we add
    if (class_exists(‘RankMath’)) {
    echo “Here”;

    }
    it works

    Hello,

    In that case, the if (class_exists(‘RankMath’)) condition did work in the first set of code you shared, it is just the code inside that scope is not working.

    Unfortunately, we can’t tell what’s the issue with the code here because we can only see a portion of the code. We need to check the entire code you have made for this.

    If you prefer, we might need to take a closer look at the settings. Please edit the first post on this ticket and include your WordPress & FTP logins in the designated Sensitive Data section.

    Please do take a complete backup of your website before sharing the information with us.
    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    No, you can try it will fresh WordPress. its not even working on data. No Code sharing required for now

    Hello,

    We have tried on a fresh installation of WordPress and we are still getting the same results from previously when we introduce an if statement to check for the presence of Rank Math on the website.

    What is not working on your website is not the if condition as echoing some text produces the expected results. It’s actually the functions inside the if statement that are not running correctly.

    If you want our team to debug the issue further you can add the login credentials and we’ll check the situation on the website.

    Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

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

The ticket ‘Add External URL in Sitemap Rankmath hook’ is closed to new replies.