Two Methods To Creating a Plugin To Run Custom Snippets

Two Methods To Creating a Plugin To Run Custom Snippets

Table of Contents

Often you would like to change how a plugin works and you put the coding in your child theme. However, if you still need to update the child theme or want to store the snippets in one place that does not have a chance of being updated then a custom plugin is recommend.

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 volt-vectors-snippets and going to create the following directory and file using my text editor or IDE:

/wp-content/plugins/volt-vectors-snippets/volt-vectors-snippets.php

In that file I am going to start with an opening php tag and the information header:

<?php
/*
Plugin Name: Volt Vectors Snippets
Plugin URI: http://voltvectors.com
Description: A Plugin to store snippets for the Volt Vectors
Author: voltvectors
Version: 1.0
Author URI: http://voltvectors.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: Volt Vectors Snippets
Plugin URI: http://voltvectors.com/wp
Description: A Plugin to store snippets for the Volt Vectors Pro
Author: brianjessee
Version: 1.0
Author URI: http://voltvectors.com/wp
*/

/**
 * Append custom meta to post content for all post types.
 *
 * @since 0.1.0
 *
 * @param string  $content The original content.
 * @param WP_Post $post    The post object.
 *
 * @return string Modified content with appended custom meta.
 */
function pngx_volt_append_meta_to_content( $content, $post ) {
    $additional_info = get_post_meta( $post->ID, 'additional_info', true );
    return $content . ' ' . $additional_info;
}
add_filter( 'pngx_volt_vectors_post_loader_content', 'pngx_volt_append_meta_to_content', 10, 2 );

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:

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 ‘<?php’ 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.