There are at least a dozen places on the web where you can find a discussion forum for help building WordPress sites including WordPress Development, WPChat, Advanced WordPress and PostStatus' Slack to name four. And on those various forums hardly a week goes by when someone does not ask a question of the form:

"How do I organize PHP code for my custom WordPress site?"

Often the person asks the differences between putting code in their theme's functions.php file or writing their own plugin. And sometimes they ask about code organization within a theme, e.g. organizing within an /includes/ directory such as discussed here.

Not about Themes for distribution!

A quick caveat here before you jump to the comments section to debate this.

I am writing about custom WordPress installations, not about code organization for a theme that you want to distribute on, via ThemeForest or via your own website. That is an entirely different topic and out of scope for this post.

Why not just stick with functions.php?

It often seems the person asking is seeking "permission" to continue keeping their code inside their theme. I am not sure why — please comment about why if you have another theory — but I think it might be they think creating a plugin will be difficult.

Nothing could be further from the truth, assuming you already know how to upload files to a webhost via SFTP, creating a plugin is trivially easy. If you build WordPress sites I expect you understand and are comfortable using plugins. If not, you need some remedial work before this post.

Otherwise, Read on.

How to create a plugin for your site

If you have custom code in your theme's functions.php file there really are only five (5) trivial steps. This assumes you have SFTP or similar access to your site's file system:

  1. Name your plugin: If your site domain is then "HardcoreWP" is a great name and 'hardcorewp' is a great slug.

  2. Create a plugin directory: Use your slug to create a directory inside your site's plugin directory, e.g.:

  3. Create an empty plugin file: Now add a .php file using using the same name as your plugin's slug, e.g.:

  4. Add a plugin header: Inside your .php file just add a comment header that names your plugin. You can put a lot of information into a plugin header but you really only need one (1) bit of information: the plugin's name:

    1. /**
    2.  * Plugin Name: HardcoreWP
    3.  */
     * Plugin Name: HardcoreWP
  5. Copy custom code from theme to plugin.
    Finally all you need to do is copy the code found in your theme's functions.php into your plugin's .php file.

And that's it!

(Oh well yes, you need to activate the plugin, but you already knew that...)


You might also want to give your plugin a "Description" so that your site's admin users will understand why you installed it:

  1. /**
  2.  * Plugin Name: HardcoreWP
  3.  * Description: Custom code by <a href="">Mike Schinkel</a> for <a href=""></a>.
  4.  */
 * Plugin Name: HardcoreWP
 * Description: Custom code by <a href="">Mike Schinkel</a> for <a href=""></a>.

Is that all?

Of course not, this is just a kickstart.

But now that you have your site's custom code in a plugin you can start to explore best practices for building WordPress plugins and learn a bit-by-bit over time.

Google is your friend, and so will be future posts here on HardcoreWP.

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" extra="">