Creating a functionality plugin for your WordPress site

One of the most common mistakes is editing functions.php of your theme file. You should almost never do that because of many reasons. Instead, you should make it a habit to create a simple “functionality plugin” for every WordPress site you build.

Let’s say your goal is simply to add a google analytics code for your “Mad Hatter Hats” site.

  • It’s a good idea to prefix your functions with a short unique word. Let’s say your prefix will be “mhat_
  • You could use a simple madhat-site-plugin.php in ./plugins folder, however its also a good idea to put it inside a folder, in case you need to put more stuff in there like custom scripts and styles.
  • You might want to add a TextDomain for your plugin in case you wanna use translatable strings like in template tag functions.

So, you’d go on and create a folder and a file,  ./wp-content/plugins/madhat-site/madhat-site-plugin.php with the content:

Plugin Name: Mad Hatter Site Functions
Description: Site Functions
Author: The Hatter
Version: 1.0
Text Domain: mhat_lang
function mhat_ga_code() {
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      ga('create', 'UA-XXXXXXXX-1', 'auto');
      ga('send', 'pageview');
add_action( 'wp_footer', 'mhat_ga_code')

You might want to check out these sources too: