Knowledge Base
Two Methods To Creating a Plugin To Run Custom Snippets
This guide will show you two different ways to do it. The first way is to manually create the file and the second is to use a plugin called Pluginception to help you create it.
For the manual method you can download a base version of the plugin to install from Github.
Manual Creation
When creating a plugin I recommend putting it into a directory in the plugin directory of your site.
In this case I am going to call the plugin coupon-creator-snippets and going to create the following directory and file using my text editor or IDE:
/wp-content/plugins/coupon-creator-snippets/coupon-creator-snippets.php
In that file I am going to start with an opening php tag and the information header:
<?php /* Plugin Name: Coupon Creator Pro Snippets Plugin URI: http://couponcreatorplugin.com Description: A Plugin to store snippets for the Coupon Creator Pro Author: brianjessee Version: 1.0 Author URI: http://couponcreatorplugin.com */
Feel free to change any of that information on your site.
Once saved go to the plugin section of your admin and find the plugin you created and activate it.
Now it is ready to add some snippets.
Add the first on after the closing comment ( */ ) and do not include another opening php tag ( <?php ) or it will cause a fatal error.
Here is an example of a plugin that adds Google Fonts to the Print View, more snippets can be added just after the first one. For the most part there are no limits on snippets to add.
<?php /* Plugin Name: Coupon Creator Pro Snippets Plugin URI: http://couponcreatorplugin.com Description: A Plugin to store snippets for the Coupon Creator Pro Author: brianjessee Version: 1.0 Author URI: http://couponcreatorplugin.com */ /* * Coupon Creator - Add Google Fonts to Print View * Version 2.1.2 */ add_action('coupon_print_meta', 'cctor_google_fonts_print_view' ); function cctor_google_fonts_print_view() { echo "<link href='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>"; echo "<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>"; }
Pluginception
If you are unsure of how that all works you can use a plugin to give you a head start on plugin creation. To start using Pluginception add it to your WordPress install:
https://wordpress.org/plugins/pluginception/
Install, activate, and then go to the Plugins->Create a New Plugin menu fill in at least the title then create and active it.
After the plugin is created, it will take you to the edit screen to add the snippet or snippets you would like.
Add the coding in the line following the closing comment ( */ ) and please be careful not to include an opening php tag ‘ with your snippet as it will cause a fatal error.
If you are not able to edit files from the WordPress admin due to a security plugin or DISALLOW_FILE_EDIT or DISALLOW_FILE_MODS this plugin will not work and you would have to create the plugin yourself using sftp or another method.
Also, once complete it is good idea to remove Pluginception from your site. Although it is secure any plugin like that should only be on your site when you are using it.