This post is inspired by the article from wpbeginner.com on creating a site-specific plugin where you can store all of your functions, instead of putting them in a child theme. If you ever change themes you will need to copy those functions to the new theme. With a site-specific plugin, your functions are still there. You can install your plugin in all the sites you build. Like all plugins, you can deactivate and uninstall it if you wish. Versions of your plugin are not discussed here.
How To
You need to create a unique php file. For example, this one could be called mike-fun-lib.php. It is highly recommended that your plugin have its own folder with probably the same name. To be able to install this plugin you will need to compress it into a zip file. Here is the format of the first part of the file.
<?php /* Plugin Name: Mike Fun Lib Plugin URI: https://begincodingnow.com/wp/site-specific-plugin Description: Mike's Function Library creates a plugin that contains my snippets of code Version: 1.0 Author: Mike Porter Author URI: https://begincodingnow.com/wp License: GPLv2 */ ?>
You will probably want to include functions that execute when your plugin is activated and deactivated. We should create a function and variable prefix, such as “mfl” or “mikefunlib” or something like this. In this case we are using “mfl”.
<?php register_activation_hook( __FILE__, 'mfl_install' ); function mfl_install() { // do something such as check the current WordPress version } register_deactivation_hook( __FILE__, 'mfl_deactivate' ); function mfl_deactivate() { // code goes here... } // write all of your code snippets below ?>
So far this plugin does nothing, but just to test it you could install it, activate it, deactivate it and then uninstall it. I created a file in Notepad++ called mikefunlib.php and copied the code from above into the file. The file is saved on my laptop and then I used 7-zip to compress it to create a zip file. I installed my plugin and activated it. So here it is showing up on the Plugins menu. It installed successfully and is activated.
To test it you could simply add a very simple function to the plugin. You could then call that function somewhere in your template file. For example, to display the text “Have a great day!” the following function could be used.
function haveagreatday() { echo "Have a great day!"; }
You could then call this function in your footer.php file, or put it in another php template file.
<?php haveagreatday(); ?>
Here are some more code snippets that could be placed into your site-specific plugin. The removal of HTML in comments was discussed in a previous post called HTML in Comments.
/* To be able to put an auto-embed URL into a Text widget just as you can in a post or page, the following two lines of code are entered here. Source: page 501 of WordPress The Missing Manual Second Edition by Matthew MacDonald. */ add_filter('widget_text', array($wp_embed, 'run_shortcode'),8); add_filter('widget_text', array($wp_embed, 'autoembed'),8); /* prevent HTML tags from being entered into comments */ add_filter('comment_text','wp_filter_nohtml_kses'); add_filter('comment_text_rss','wp_filter_nohtml_kses'); add_filter('comment_excerpt','wp_filter_nohtml_kses');
Shortcodes
A shortcode is a special tag that you can enter into a post that gets replaced with different content. The content could be just about anything from some text to a gallery of photos. A shortcode looks similar to an HTML tag, but is enclosed with square brackets instead of angle brackets. Many plugins use shortcodes. You can even create your own shortcodes. To create your own shortcode define a function that outputs the actual content and put that function in one of two places: functions.php file or in a custom plugin. Here is a function I created and put it into my site-specific plugin:
/* When the WordPress writer types mikeshortcodehelp in a post or a page, the string will be returned */ add_shortcode( 'mikeshortcodehelp', 'mfl_shortcodehelp'); function mfl_shortcodehelp() { return '<p>Creating shortcodes in WordPress is easy.</p>'; }
To get the very last line of this post, I typed the second last line into the Text editor of this post. Interestingly, to get the second last line of this post to display correctly I couldn’t just type my short code because it would immediately be processed and it would show the comment that creating shortcodes is easy. How did I get the angle brackets to appear without using angle brackets? I used HTML character references.
[mikeshortcodehelp]
[mikeshortcodehelp]