• Resolved wowlayers

    (@wowlayers)


    I have 3 domains that share the same files and single database. So when a page/post cache happens, all HTML URLs are cached with the URL from which the request is coming from.

    But if another user access for example the 2nd domain URL, then the cached page will work incorrectly, because the page source contains all URLS from the other domain. I hope this makes sense ??

    Is there a way to intercept the cached static files (using a filter) generated by WP Super Cache, right before they are served to visitors, so I can replace the domain dynamically based on the actual request?

    I looked in the plugin files for hooks, tried several, could not get anything to work.

    Thanks for your help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter wowlayers

    (@wowlayers)

    I tried this filter, I am almost sure this is the one, but it doesn’t log anything.

    add_filter('wp_cache_served_cache_file', 'modify_served_cache_file', 10, 1);

    function modify_served_cache_file($cache_file) {
    // Inspect the cache file being served
    error_log("Serving cache file: " . $cache_file);

    // Modify the cache file path or manipulate its content if needed
    // Example: Read and replace content dynamically
    if (file_exists($cache_file)) {
    $cache_data = file_get_contents($cache_file);

    // Example: Replace the domain dynamically based on the request
    $current_domain = $_SERVER['HTTP_HOST'];
    $cached_domain = 'primary-domain.com'; // Replace with your cache domain

    // Replace domain in cached data
    $cache_data = str_replace($cached_domain, $current_domain, $cache_data);

    // Save the modified content back to the file (optional)
    file_put_contents($cache_file, $cache_data);
    }

    // Return the modified or original cache file path
    return $cache_file;
    }

    This is my WP_DEBUG Setup

    define( 'WP_DEBUG', true );
    define( 'WP_DEBUG_LOG', true );
    define( 'WP_DEBUG_DISPLAY', false ); // Prevents errors from displaying on the frontend
    @ini_set( 'log_errors', 1 );
    @ini_set( 'display_errors', 0 ); // Prevents errors from displaying on the frontend

    WP Super Cache is set to operate in Simple Mode.

    Any ideas would help ??

    Plugin Support Stef (a11n)

    (@erania-pinnera)

    Hi there, @wowlayers,

    Thanks for explaining your issue and sharing your script attempt to achieve what you requested ??

    This goes a bit beyond my technical skills, so I have asked our developers to take a look at your request to advise you accordingly. We’ll get back to you as soon as we heard from them!

    Plugin Contributor Pete (a11n)

    (@dilirity)

    Hello there @wowlayers!

    Thank you for the code example and information about your setup.

    I think you’re on the right track and you’re using the right filter! Is it possible that you’re adding it to your theme?

    If so that wouldn’t work, because if WordPress serves a cached file, it stops executing code that would normally execute on a non-cached request. In this case, any PHP code you add to the theme (or the theme’s PHP code) wouldn’t get executed.

    Thankfully, WP Super Cache allows for a workaround.

    First, you’ll need to modify your snippet a bit. Change this line:

    add_filter('wp_cache_served_cache_file', 'modify_served_cache_file', 10, 1);

    to this:

    add_cacheaction('wp_cache_served_cache_file', 'modify_served_cache_file' );

    This will tell WP Super Cache to run your code.

    After that, create a new .php file inside wp-content/plugins/wp-super-cache/plugins/, and add the updated code to it.

    This will allow WordPress to execute your code and do the replacements!

    Fingers crossed this works.

    If it doesn’t, leave a comment so we can continue investigating.

    Cheers!

    • This reply was modified 1 month, 3 weeks ago by Pete (a11n). Reason: clarify file creation
    Thread Starter wowlayers

    (@wowlayers)

    @dilirity thanks for taking the time to write.

    In the end it seems WP Super Cache, creates separate folders for each domain, so my issue is resolved ??

    Plugin Support Stef (a11n)

    (@erania-pinnera)

    Hey @wowlayers,

    We’re glad to hear that your issue got eventually solved and you seem to be all set now ??

    I’m going to mark this thread as solved. If you have any further questions or need more help, you’re welcome to open another thread here. Cheers!

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.