create a custom wordpress plugin from scratch

 What is a WordPress Plugin?

If you want to add unique features and functionalities to your WordPress website to deliver a unique experience to your users even when changing a theme, you want that code to reside outside of the theme, and that’s where custom WordPress Plugin Development is come into play.

Plugins are one of the coolest things that happened to WordPress as they make your WordPress site more powerful with additional features and functionality on your WordPress website. A plugin is a piece of code that adds functionality to your site beyond what WordPress offers you at its core.  Although there are more than 50,000 plugins in the massive WordPress Plugin repository to help you add the feature or functionality to your site, they sometimes don’t meet your business requirements or goals. In such a situation, custom WordPress Plugin Development will help you.

WordPress Plugin Development Steps

CREATE THE PLUGIN FOLDER AND FILE

To get started creating a new plugin, follow the steps below.

 Navigate to the WordPress installation’s wp-content directory.

  • Open the plugins directory.
  • Create a new directory and name it after the plugin (e.g. plugin-name).
  • Open the new plugin’s directory.
  • Create a new PHP file (it’s also good to name this file after your plugin, e.g. plugin-name.php)
Only one file in the plugin’s folder should have the header comment — if the plugin has multiple PHP files, only one of those files should have the header comment.

After you save the file, you should be able to see your plugin listed in your WordPress site admin dashboard

Open the plugin-name.php file and insert a comment header (similar to the comment header in the theme’s style.css). The minimum comment header can have only one field—the plugin name—but we’ll add a few more fields so it shows up like all other plugins.

/**

* Plugin Name: My First Plugin
* Plugin URI: http://codesanitize.com
* Description: The basics with this plugin
* Version: 1.0.0
* Author: Code Sanitize
* Author URI: http://codesanitize.com

*/

A full header could consist of all these fields (you can read up on each field’s definition in wordpress.org’s Plugin Development Handbook)

Plugin Name, Plugin URI, Description, Version, Requires at least, Requires PHP, Author, Author URI, License, License URI, Text Domain, Domain Path, Network.

Plugin Architecture

The architecture, or code organization, you choose for your plugin will likely depend on the size of your plugin.

For small, single-purpose plugins that have limited interaction with WordPress core, themes or other plugins, there’s little benefit in engineering complex classes; unless you know the plugin is going to expand greatly later on.

For large plugins with lots of code, start off with classes in mind. Separate style and scripts files, and even build-related files. This will help code organization and long-term maintenance of the plugin.

A clear folder structure helps you and others working on your plugin keep similar files together.

Here’s a sample folder structure for reference:

/plugin-name
plugin-name.php
uninstall.php
/languages
/includes
/admin
     /js
    /css
   /images
/public
   /js
   /css
   /images


Below is the example of Hello World WordPress plugin code

Add below code in plugin-name.php file and activate plugin from admin dashboard


<?php
/**
Plugin Name: Hello World By codesanitize.com
Plugin URI: http://codesanitize.com
Author: Code Sanitize
Author URI: http://codesanitize.com
Description: This plugin add "hello world" text at the bottom of every posts.
Version: 1.0
*/
function hello_world_func($content){
                 return $content."<span>Hello World</span>";
}
add_filter('the_content', hello_world_func);
?>

Previous
Next Post »