Additional target URL in sunny plugin for cloudflare full page cache purge

Category : WordPress

This is a very simple tutorial for non coders / developers to add additonal target URLs in the sunny plugin. The main use of sunny plugin is for purging of “full page cache” upon occurence of different instances.

There are 2 types of URLs in the sunny plugin

  1. Related URL : The set of URLs which should be purged on update/edit/create any content or post on the website. Eg. categories pages.
  2. Target URL : This is the URL which is purged “everytime” any of the content is updated. By default, the plugin only contains the site URL i.e the homepage of the website.

So to add more Target URL i.e the URL of the website which should always purge on any update on site, locate the following file in your website files :

Purger.php

File location : public_html/wp-content/plugins/sunny/src/Caches/Purger.php

Replace public_html with your website home directory.

We need to modify a function in this file.

public function execute(PurgeCommand $command)
    {
        $randomNoticeId = 'sunny_purger_execute_' . wp_hash(date('c') . random_int(10, 15));

        // Translators: %1$s is the reason to purge.
        $noticeMessageFormat = __('<b>Sunny</b>: Purge initiated.<br/>Reason: %1$s', 'sunny');
        $noticeMessage = sprintf(
            $noticeMessageFormat,
            $command->getReason()
        );

        $this->notifier->enqueue($randomNoticeId, $noticeMessage);

        $batches = array_chunk(
            $command->getUrls(),
            30
        );

        foreach ($batches as $batch) {
            $this->cache->purgeFiles(
                ...$batch
            );
        
        }
        
        // <---Added by BaseZap  [email protected] 
       //Add your extra target URL here      
        $custom_url = "https://www.basezap.com/blog/";
        $this->cache->purgeFiles($custom_url);
        //-----BaseZap-----> 
    }

Save the file! If you have more target URLs then simply add the same set of code or can make an array of custom_url for the set of urls.

Basically, the purgeFiles function is called for purging the set of URLs which are usually defined in the batch variable. We added extra code to purge the URL by calling the same function with our extra target URL.