• Resolved drmrgood

    (@drmrgood)


    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]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Rank Math Support

    (@rankmathteam)

    Hello @drmrgood,

    Thank you for contacting support.

    That filter is not designed to add additional URLs to the sitemap, is designed to modify URLs already available.

    To add new URLs you have to use the following filter: https://rankmath.com/kb/filters-hooks-api-developer/#add-extra-urls-in-sitemap

    Alternatively, if you want to create a brand new sitemap with all the PDFs you can follow the procedure here: https://rankmath.com/kb/custom-sitemaps/

    Don’t hesitate to get in touch if you have any other questions.

    Thread Starter drmrgood

    (@drmrgood)

    I successfully added all .pdf files from a folder where they are located but I slightly modified the code for custom-sitemap.php file in this link:
    https://rankmath.com/kb/custom-sitemaps/


    New code is:



    <?php

    namespace RankMath\Sitemap\Providers;

    defined( ‘ABSPATH’ ) || exit;

    class Custom implements Provider {

    public function handles_type( $type ) {
    return ‘custom’ === $type;
    }

    public function get_index_links( $max_entries ) {
    return [
    [
    ‘loc’ => \RankMath\Sitemap\Router::get_base_url( ‘custom-sitemap.xml’ ),
    ‘lastmod’ => ”,
    ]
    ];
    }

    public function get_sitemap_links( $type, $max_entries, $current_page ) {
    $links = [];

    // Define the folder where the files are stored
    $folder = ‘/manuals/’;

    // Get the list of files in the folder
    $files = glob( ABSPATH . $folder . ‘*’ );

    // Loop through each file
    foreach ( $files as $file ) {
    // Get the file name
    $file_name = basename( $file );

    // Get the file URL
    $file_url = home_url( $folder . $file_name );

    // Add the file URL to the links array
    $links[] = [ ‘loc’ => $file_url ];

    // Check if the maximum number of entries is reached
    if ( count( $links ) >= $max_entries ) {
    break;
    }

    }

    return $links;
    }

    }


    If someone needs something similar just change /manuals/ according to the folder you need.

    • This reply was modified 1 year, 1 month ago by drmrgood.
    Plugin Support Rank Math Support

    (@rankmathteam)

    Hello @drmrgood,

    We are glad that you were able to create a new sitemap for the PDF files, and thank you for sharing the solution here.

    Please do not hesitate to let us know if you need our assistance with anything else.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add .pdf files in a folder to the sitemap file’ is closed to new replies.