How To Create Your Own WordPress Plugin – Best Practice

HL News • February 18, 2014

Creating a site-specific plugin is a best practice when developing in WordPress. It travels with you no matter what theme you are using, and is easy to troubleshoot by disabling if your theme develops issues down the road.

I used to put ALL of my functions in the functions.php page regardless of if they were theme-specific or site-specific, but now I solely use plugins whenever I can, when the function is NOT theme-specific. This helps keep my functions.php page light and short, and easy to sort through.
Basically, any code that you put in your functions.php file that is NOT theme-specific, you should make a plugin for.

Here is the standard layout for a beginning plugin:


/*
Plugin Name: Your Plugin Name
Description: Description of what the plugin does
*/
/* Add Your Functions Below this Line */

/* Stop Adding Functions Below this Line */
?>

I like to save my plugin with the same name that I use for my ‘Plugin Name’, and then I put that plugin-name.php file in a folder with the same name. This saves a TON of headaches later, searching through randomly named plugins, trying to find the one you’re looking for.

Then, just upload that folder to your Plugins directory and activate it through your WordPress Dashboard.

Here’s an example of creating a ‘Encrypt Email’ plugin, as talked about in our post ‘Top 5 Most Useful WordPress Functions & Plugins‘:


/*
Plugin Name: Encrypt Email
Description: Convert email address characters to HTML entities to block those pesky spam bots.
*/

// Hide Email from Spam Bots using a short code place this in your functions file

function HideMail($atts , $content = null ){
if ( ! is_email ($content) )
return;

return '' .antispambot($content). '';
}

// add shortcode for posts and pages
add_shortcode( 'email','HideMail');

// add shortcode for widgets
add_filter('widget_text', 'do_shortcode');

?>

I saved the code above as encrypt-email.php in a folder called encrypt-email and uploaded it my Plugins directory. Now, regardless of the theme that I use, I can insert the shortcode in posts, pages, or widgets, or call the php embed code within any template file.

Happy Coding!