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:

<?php
/*
Plugin Name: Mad Hatter Site Functions
Description: Site Functions
Author: The Hatter
Version: 1.0
Text Domain: mhat_lang
*/
function mhat_ga_code() {
	?>
	<script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
    
      ga('create', 'UA-XXXXXXXX-1', 'auto');
      ga('send', 'pageview');
    
    </script>
    <?php
}
add_action( 'wp_footer', 'mhat_ga_code')
?>

You might want to check out these sources too:

* http://codex.wordpress.org/Child_Themes
* https://css-tricks.com/wordpress-functionality-plugins/
* http://wpninjas.com/how-to-create-a-simple-wordpress-plugin/