Buy Shared Hosting only just ₹ 85 per month.

Contact Info

1234, Basant Vihar, Near Aklank Public School, Kota (Rajasthan)

+91-9119220608

info@whoischoice.com

Get Started
Recommended Services
Supported Scripts
WordPress
Hubspot
Joomla
Drupal
Wix
Shopify
Magento
Typeo3
How to create cache for wordpress website without using plugin

Creating a cache for a WordPress website without using a plugin involves implementing server-side caching mechanisms. Here’s a step-by-step guide to help you do this:

  1. Enable Browser Caching with .htaccess
  1. Open your .htaccess file (usually located in the root directory of your WordPress installation).
  2. Add the following lines to enable browser caching:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg “access 1 year”
    ExpiresByType image/jpeg “access 1 year”
    ExpiresByType image/gif “access 1 year”
    ExpiresByType image/png “access 1 year”
    ExpiresByType text/css “access 1 month”
    ExpiresByType text/html “access 1 month”
    ExpiresByType application/pdf “access 1 month”
    ExpiresByType text/x-javascript “access 1 month”
    ExpiresByType application/x-shockwave-flash “access 1 month”
    ExpiresByType image/x-icon “access 1 year”
    ExpiresDefault “access 2 days”
</IfModule>

  1. Enable Server-Side Caching with PHP
  1. Create a cache directory:
    • Create a directory named cache in the root directory of your WordPress installation.
    • Make sure this directory is writable by setting the correct permissions (chmod 755 cache).
  2. Modify your theme’s header.php file:
    • Add the following PHP code at the top of the file to start the caching process:

<?php
$cachefile = ‘cache/’ . md5($_SERVER[‘REQUEST_URI’]) . ‘.html’;
$cachetime = 18000; // Time in seconds to keep the cache file
 if (file_exists($cachefile) && time() – $cachetime < filemtime($cachefile)) {
    // Serve cached content
    readfile($cachefile);
    exit;
}
ob_start(); // Start output buffering
?>

  1. Modify your theme’s footer.php file:
    • Add the following PHP code at the bottom of the file to save the output to the cache file:

<?php
$cached = fopen($cachefile, ‘w’);
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
?>

  1. Optimize Database Caching

While server-side file caching and browser caching are helpful, optimizing database queries can significantly enhance performance.

  1. Edit your wp-config.php file:
    Add the following line to enable MySQL query caching:

define(‘WP_CACHE’, true);

  1. Optimize your MySQL configuration:
    Edit your MySQL configuration file (usually cnf or my.ini) to include:

[mysqld]
query_cache_type = 1
query_cache_size = 64M

  • Restart your MySQL server to apply these changes.
  1. Use a CDN (Content Delivery Network)

A CDN can cache your static assets (like images, CSS, and JavaScript files) and serve them from locations closer to your users. Some popular CDNs include Cloudflare, Amazon CloudFront, and MaxCDN. Configuring a CDN typically involves:

  1. Signing up for a CDN service.
  2. Configuring your CDN to pull content from your website.
  3. Updating your DNS settings to point to your CDN.
  1. Leverage Object Caching

If your hosting environment supports it, you can implement object caching using a service like Memcached or Redis.

  1. Install and configure Memcached or Redis on your server.
  2. Add the following lines to your wp-config.php file to integrate with WordPress:

// For Memcached
define(‘WP_CACHE’, true);
$memcached_servers = array(
    ‘default’ => array(
        ‘127.0.0.1:11211’
    )
);
// For Redis
define(‘WP_REDIS_HOST’, ‘127.0.0.1’);
define(‘WP_CACHE’, true);

By following these steps, you can effectively create a caching system for your WordPress website without using on plugins. This approach can significantly improve your site’s performance and load times.