Blog Post default image

Do you want to edit WordPress’s “index.php” and not have it be over written every time WordPress updates? Well There is a solution to this problem and it’s stupid simple.

Before you start chomping at the bit and commenting that you “SHOULD NOT” for any reason edit core WordPress files, let me get to my next point.

There are perfectly valid reasons for wanting to edit WordPress’s “index.php” file, usually it is a single line of code to include a class or set of PHP functions that you want at the very highest level of your website or application.

This solution lets you run code before WordPress begins, while not editing any WordPress Core files!

Lets look at a quick example of what I use on some websites to allow for basic MVC and non WordPress ajax calls, whilst also sitting WordPress in the root directory of set website.

Edited: index.php

	/* Higher level private functions and classes */
	include($_SERVER['DOCUMENT_ROOT'] .'/classes/global.php');
	/**
	 * Tells WordPress to load the WordPress theme and output it.
	 *
	 * @var bool
	 */
	 define('WP_USE_THEMES', true);
	/** Loads the WordPress Environment and Template */
	require( dirname( __FILE__ ) . '/wp-blog-header.php' );

Note the top line which goes and defines a global class for me to use, maybe for you it’s a class or file full of helper functions you don’t want to have to replicate into all your different themes functions.php files, in my case it simply defines a set of ajax and mvc functions to let me create super FAST ajax calls outside of WordPress!

Why do I need to create super fast ajax calls outside of WordPress? Well that is because you can’t create super fast inside of WordPress. If you touch the core WordPress object pro-grammatically you are basically f&*ked and your ajax requests are around 1 second each, not acceptable.

Now, whatever your reason for editing “index.php” is, you are going to run into a serious problem. WordPress will over write index.php every time WordPress updates, if you have automatic updates on, that means it will break your custom edit each time WordPress updates.

Sure you can turn off automatic updates on WordPress, but then you have to manually update each WordPress instance, and it’s still going to overwrite the index.php file. This also adds a second step of then manually needing to copy your custom index.php back into the root each time. This is a nightmare if you control multiple WordPress sites that use this technique, think of it as a “super massive black hole” of wasted time.

How do we solve this?

Your first attempt might be based around, write protecting your edited “index.php” file via chmod permissions, try to set the edited file “index.php” to 444 and WordPress won’t be able to touch it.

WRONG for two reasons.

Firstly: WordPress permissions give it full ownership over your files and no matter how you try to write protect them via chmod, index.php will always get over wrote, back to WP default. Essentially you can’t write protect index.php.

Secondly: You really should not be editing any core WordPress files anyway, this is a general rule of thumb to save yourself time and frustration.

Ok enough blah blah, show me the solution!

Step 1: .htaccess

In your .htaccess file in your root directory (or where ever you put WordPress), copy and paste the code below.

	# Load blog.php first, if not found, load index.php
	DirectoryIndex blog.php index.php	

	// Rewrite Rules ...

The DirectoryIndex parameter will simply tell the server not to load index.php first, but instead load a file of your choosing, in my case blog.php. I want my website to load blog.php first and foremost above all other things, because that file contains my edited versions of the WordPress index.php.

Step 2: .htaccess

Change the WordPress snippet in your .htaccess file, from this:

	# BEGIN WordPress
	
	RewriteEngine On
	RewriteBase /
	RewriteRule ^index\.php$ - [L]
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule . /index.php [L]
	
	# END WordPress

To this:

	# BEGIN WordPress
	
	RewriteEngine On
	RewriteBase /
	RewriteRule ^blog\.php$ - [L]
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule . /blog.php [L]
	
	# END WordPress

You have now effectively told WordPress the very first thing in your root directory to look for and load via WordPress is blog.php.

Yes, really, it’s this easy, no editing core files, no auto update nightmares, just smooth sailing and custom code before WP loads.

Your blog.php file:

Can now look like anything you want it to without fear of it getting reset by a WordPress update.

	// Your code here
	echo "You can now safely do whatever you want before WordPress even starts loading";
	/**
	 * Tells WordPress to load the WordPress theme and output it.
	 *
	 * @var bool
	 */
	 define('WP_USE_THEMES', true);
	/** Loads the WordPress Environment and Template */
	require( dirname( __FILE__ ) . '/wp-blog-header.php' );

Note: Essentially now you don’t care what WordPress does with index.php, it can overwrite it on update and it won’t affect your website at all.

Trust me this works, on any WordPress updates, WP will not touch your blog.php file, as it iss not a core WordPress file of any kind. You can technically name this file what ever you wish, I just called it blog.php for the examples.

After that, you are good to rock and roll, you can rest easy knowing your sites won’t go down, lose functionality or cost you an excessive amount of manual labor each time WordPress updates itself, plus you can leave automatic updates on, again saving you time and hassle!

Enjoy 🙂

Tags: ,

Post a Comment