wordpress code

WordPress code I always use on new installs

I set up a lot of WordPress sites and I keep text files of the bits of code I add to the .htaccess file and config file on all new installs.

To keep WordPress going smoothly, there are a few bits of code I add to files when I install WordPress on a new domain.

Increase Memory Limit

To increase the allowed memory size from the standard 40M to 256M I use the following code in wp-config.php (usually in the /public_html/ folder:

define('WP_MEMORY_LIMIT', '256M');

Replace the 256M with 96/128/256 but I find that 128M will suffice for most sites.

Disable WP-Cron

Until recently, I left the wp-cron to handle daily tasks with WordPress. I was advised by my hosts to switch to cPanel to handle cron jobs, this is more efficient and improves performance and reduces server load significantly.

In the same file as before, wp-config.php enter the code below. I usually put it after the database collate information [define(‘DB_COLLATE’, ”);], don’t forget to save:

define('DISABLE_WP_CRON', 'true');

This will disable the wp-cron from running – you must enable the file to be run by your server. Login to your host backend, usually cPanel, click on Cron Jobs (Advanced section) and enter the command:

/usr/local/bin/php ~/public_html/wp-cron.php >/dev/null 2>&1

The first part /usr upto php depends on your host. They will usually have a post in their knowledgebase or you can message support for advice. The second part is the actual location of the wp-cron.php file. The >/dev/null 2>&1 just disables e-mail notifications, you will receive loads if left active.

If like me, you install WordPress into the root directory then [username]/public_html/wp-cron.php should be fine. My host setup may differ to yours and I don’t need to specify the username.

After you enter that, you can choose the times to run under common settings, my host suggested twice every hour, some suggest once an hour or once every 6 hours if you don’t have much traffic or make regular changes.

For The Newsletter plugin I changed the frequency to once every 5 minutes as that is the recommended time for that plugin.

Limit Post Revisions

I found that I would have tens of revisions to a single post, all of these would be clogging up my database with useless data. To limit them just enter this code into wp-config.php

define ('WP_POST_REVISIONS', 3);

This will limit the maximum revisions to just 3 rather than loads and loads, 3-5 should be more than enough.

SSL Redirect

As all of my WordPress sites have SSL installed by default and I install to the base url without the www I add the following code to my .htaccess file to divert everything to the base url:

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{HTTPS} off
 RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 </IfModule>

I made another post on this here where I had issues with icons

Expires Headers for WordPress

Add this to your .htaccess file (Apache server not Nginx) using the file manage in cPanel, click on settings and check show hidden files as files with a preceding . are usually hidden.

<IfModule mod_expires.c>
  ExpiresActive On

  # Images
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType image/x-icon "access plus 1 year"

  # Video
  ExpiresByType video/webm "access plus 1 year"
  ExpiresByType video/mp4 "access plus 1 year"
  ExpiresByType video/mpeg "access plus 1 year"

  # Fonts
  ExpiresByType font/ttf "access plus 1 year"
  ExpiresByType font/otf "access plus 1 year"
  ExpiresByType font/woff "access plus 1 year"
  ExpiresByType font/woff2 "access plus 1 year"
  ExpiresByType application/font-woff "access plus 1 year"

  # CSS, JavaScript
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"

  # Others
  ExpiresByType application/pdf "access plus 1 month"
  ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
</IfModule>

Redis Password

If you are having issues with Redis connections for server caching, you can manually enter the code into your wp-config.php file.

Enter this code somewhere in the file.

/** Redis Config **/
define( 'WP_REDIS_PASSWORD', 'yourpasswordgoeshere' );

I usually insert the password into the LiteSpeed Cache plugin but sometimes it does not connect so I enter the code into the wp-config.php file.

Auto Update WordPress

I prefer to let WordPress auto-update sites, sometimes the default is disabled.

Just add this code to the wp-config.php file

define( ‘AUTOMATIC_UPDATER_DISABLED’, false );

Similar Posts