How to make a custom WordPress plugin for advanced customization options

In my other post on how to stop most WordPress comment spam dead in the preprocessing, I posted PHP code that needs to be put inside a custom plugin to execute.

Customizing WordPress with your own code in your own plugin is often a better idea than adding yet another third party plugin bogging down your side with even more overhead and mysql queries. If you don't already have a custom plugin so you can do advanced WordPress customization, here's a tutorial. It's a lot easier than people think.

<?php
/**
 * Plugin Name: My Custom Plugin
 * Description: Allows the blog owner to easily add custom plugin code to customize his WordPress install
 * Version:     1.0
 * Author:      Admin
 * Text Domain: custom-plugin
 */

// code goes below this point and before the closing ?>

?>

Simply make a file named custom.php in a folder named custom, and paste the above code in there.

Next turn the folder into a ZIP file also named custom.zip.

Now you can go to WordPress admin, select Plugin -> Add New and then Upload Plugin and finally click the buttons to install and activate it.

That's it. Now you can add custom code to your WordPress install without having to meddle with theme files (these get overwritten when updated). Just go to Plugins -> Plugin File Editor and select your My Custom Plugin.

Custom plugin customization/performance examples

Many WordPress installs use plugins like Insert Headers and Footers to insert things like extra meta tags or google analytics code, etc. This adds overhead and extra mysql queries that you can get rid of now that you can do the job directly.

/* add stuff to your blog's html header */
add_action("wp_head", function ()
{
	// echo''; to print directly into the header, like this example that spits out a stylesheet tag
	echo '<link rel="stylesheet" href="/wp-content/plugins/custom/custom.css" media="all" />';

	// tell search engines not to index sections of your site where the url path starts with a certain pattern - in this case, tags
	$start_of_url = "/tag/";
	if (substr($_SERVER['REQUEST_URI'], 0, strlen($start_of_url)) == $start_of_url)
	{
		echo '<meta name="robots" content="noindex">';
	}
} );

Important caveat

If you add faulty code to your plugin, WordPress will throw an error and empty the custom plugin's .php file as an emergency means of disabling it.

A typical fatal error from screwing up while editing a WordPress plugin says "Your PHP code changes were rolled back due to an error on line 460 of file wp-content/plugins/custom/custom.php. Please fix and try saving again. syntax error, unexpected 'for' (T_FOR), expecting '('" but can also read differently like "Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP."

You'll notice that if you fix the buggy code and hit the Update File button again, WordPress will just warn that it's an "invalid_plugin."

Even the best programmers make mistakes all the time. There are two ways to fix what just happened:

  • The best solution: Open FTP or whichever other file management system you use, navigate to /wp-content/plugins/custom/ and edit the code back into custom.php -- or upload a working version of the file. Now your plugin works again.
  • Much worse solution: If you somehow don't have access to the files for your own WordPress install, you can rename the plugin to something else like custom-2 and reupload and install the new ZIP of the new iteration of the plugin. Needless to say this creates new files every time you have an error in your code, but since they're empty and disabled plugins, it's not a huge problem.

Any questions or comments, or examples of cool customizations that you've done, add them below.

Leave a Comment