Client Friendly – PDF Update

Tech Articles | April 11, 2025 | Blog, Coding, Wordpress

One of the Facebook groups I am a member of asked this question: They are publishing weekly menus, and Google has archived the old menus. They were asking about this, but the issue stems from the fact that they upload a PDF every week to the upload folder and leave it there for Google to index.

So, the solution to situations like this is to have a single file location and update it. Someone else suggested this, and then the original poster said they can’t be because of the dated media folders (I remove these by default), and they then said to update this via FTP instead then.

The advice is solid, but the response from the original poster is valid: “This isn’t client-friendly.” I agree, so this quick article is about how I would approach this situation. I would code a custom solution to upload any PDF to the same place and name on a weekly basis, and if I didn’t want it indexed, I would add it to the robots.txt file.

So I agree that an FTP upload isn’t client-friendly, so I think I’d create an interface for that, something like the below:

I’ve done this using a snippet that bypasses the typical media uploads, accepts any named PDF and deletes and replaces the previous version in the top level of wp-contents/uploads/ this was there is no client error on naming, its only ever one file and the same link can be used everywhere and it constantly serves the most current version (making sure to either clear cache or bypass cache for this file or you could add a cache clearing function to the upload too)

The code I have used for the above example is fully working and you can view it below:

<?php
/**
 * Plugin Name: Change Weekly Menu
 * Description: Upload any PDF and rename it to weekly-menu.pdf in the main uploads folder.
 * Version: 1.0
 */

add_action('admin_menu', function () {
    add_menu_page('Change Menu', 'Change Menu', 'manage_options', 'change-weekly-menu', 'cwm_upload_page', 'dashicons-media-document');
});


// Handle the file upload
function cwm_handle_upload() {
    if (!current_user_can('manage_options') || !check_admin_referer('cwm_upload_nonce')) return;

    if (!empty($_FILES['weekly_menu_file']['tmp_name'])) {
        $uploaded_file = $_FILES['weekly_menu_file'];
        $tmp_path      = $uploaded_file['tmp_name'];

        // Check it's a PDF
        $filetype = wp_check_filetype_and_ext($tmp_path, $uploaded_file['name']);
        if ($filetype['ext'] !== 'pdf') {
            echo '<div class="notice notice-error"><p>Only PDF files are allowed.</p></div>';
            return;
        }

        // Force direct path to wp-content/uploads
        $upload_base = WP_CONTENT_DIR . '/uploads';
        $destination_path = trailingslashit($upload_base) . 'weekly-menu.pdf';

        // Make sure uploads folder exists
        if (!file_exists($upload_base)) {
            wp_mkdir_p($upload_base);
        }

        // Delete old version
        if (file_exists($destination_path)) {
            if (!unlink($destination_path)) {
                echo '<div class="notice notice-error"><p>Could not delete the old menu. Check file permissions.</p></div>';
                return;
            }
        }

        // Move the uploaded file
        if (move_uploaded_file($tmp_path, $destination_path)) {
            echo '<div class="notice notice-success"><p>New menu uploaded successfully to <code>wp-content/uploads/weekly-menu.pdf</code>!</p></div>';
        } else {
            echo '<div class="notice notice-error"><p>Upload failed. Could not move file. Check permissions.</p></div>';
        }
    }
}




// Render admin page
function cwm_upload_page() {
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        cwm_handle_upload();
    }

    ?>
    <div class="wrap">
        <h1>Change Weekly Menu</h1>
        <form method="post" enctype="multipart/form-data">
            <?php wp_nonce_field('cwm_upload_nonce'); ?>
            <p>
                <label for="weekly_menu_file">Select a PDF to upload (it will be renamed to <code>weekly-menu.pdf</code>):</label><br>
                <input type="file" name="weekly_menu_file" accept="application/pdf" required>
            </p>
            <p>
                <button type="submit" class="button button-primary">Upload & Replace Menu</button>
            </p>
        </form>
        <?php
        $upload_dir = wp_upload_dir(['subdir' => '']);
        $file_path = trailingslashit($upload_dir['basedir']) . 'weekly-menu.pdf';
        $file_url = trailingslashit($upload_dir['baseurl']) . 'weekly-menu.pdf';
        if (file_exists($file_path)) {
            echo '<p>Current menu: <a href="' . esc_url($file_url) . '" target="_blank">View weekly-menu.pdf</a></p>';
        }
        ?>
    </div>
    <?php
}


PHP

In my example above, I would then add the following to my robots.txt file

User-agent: *
Disallow: /wp-content/uploads/weekly-menu.pdf

I asked them not to index this file, and I solved both issues: easy, quick uploads with no need to change the links, and it shouldn’t be indexed and out of date anymore.

If you prefer a quick video overview of this problem and solution can be viewed below:

Support the Author

buy me a coffee
Really Useful Plugin Logo
Appoligies for any spelling and grammer issue. As a dyslexic i need to rely on tools for this they like me are not perfect but I do try my best