Add .pdf files in a folder to the sitemap file
-
I have a lot of .pdf files in one folder. How can I add those files to the sitemap?
I tried with this code but it doesn’t work:
// Add custom URLs to the sitemap
add_filter( ‘rank_math/sitemap/entry’, ‘add_custom_urls_to_sitemap’, 10, 2 );
function add_custom_urls_to_sitemap( $url, $type ) {
// Check if the type is post
if ( ‘post’ === $type ) {
// Define the custom folder where the PDF files are stored
$custom_folder = ‘/wp-content/uploads/custom-folder/’;
// Get the list of PDF files in the custom folder
$pdf_files = glob( ABSPATH . $custom_folder . ‘*.pdf’ );
// Loop through each PDF file
foreach ( $pdf_files as $pdf_file ) {
// Get the file name
$file_name = basename( $pdf_file );
// Get the file URL
$file_url = home_url( $custom_folder . $file_name );
// Add the file URL to the sitemap array
$url[] = array(
‘loc’ => $file_url,
‘lastmod’ => date( ‘c’, filemtime( $pdf_file ) ),
‘changefreq’ => ‘monthly’,
‘priority’ => 0.6,
);
}
}
// Return the modified sitemap array
return $url;
}
I tried to refresh my sitemap as you described in many of your replies here in the Support section by changing the number of the links in sitemap settings and saving and also by going to settings->permalinks and saving, but nothing changes – my sitemap file doesn’t refresh.The page I need help with: [log in to see the link]
- The topic ‘Add .pdf files in a folder to the sitemap file’ is closed to new replies.