Programming

Set your own cache clean url

Do you want to give subscriber privilege  to delete the cache created by supercache plugin or any other plugins ? if you say “Yes” , follow this article .

 

    1. Create your template file.
      /*
      * Template Name: Delete cache folder
      */
      
      
    2.  Create a function that recursively delete the files and folders ,  in your functions.php file
      /*
       * php delete function that deals with directories recursively
      */
      function delete_files($str) {
          //It it's a file.
          if (is_file($str)) {
              //Attempt to delete it.
              return unlink($str);
          }
          //If it's a directory.
          elseif (is_dir($str)) {
              //Get a list of the files in this directory.
              $scan = glob(rtrim($str,'/').'/*');
              //Loop through the list of files.
              foreach($scan as $index=>$path) {
                  //Call our recursive function.
                  delete_files($path);
              }
              //Remove the directory itself.
              return @rmdir($str);
          }
      }
      
    3.  Provide your cache folder directory in the template file you created. My cache folder is inside the wp-content folder. Call the function in the same template file.
      $cacheFolder = WP_CONTENT_DIR .'/cache/';
      if(function_exists('delete_files')) {
      delete_files($cacheFolder);
      echo 'Cache has been cleaned!!';
       }
    4. Create a new page in your dashboard and and load the template file,  just created.
    5. Access the page url,  cache of the site site is cleard with the ‘Cache has been cleaned’ message . i.e. http://your-site.com/delete-cache
    6. Check your cache folder, you don’t see any files. All the files are removed from the cache folder

This  is very useful, when user other than admin wants  privilege  to delete the cache of the website.

Views: 30

Standard