Right after its introduction, WordPress created a sensation in the web development industry as an outstanding blogging platform. Soon the time changed and advancement in technology led to the improvement in the functionality of WordPress; not just only as a blogging platform but also as a leading CMS for web development. No sooner, it has become the necessity of all web developers and programmers looking for some remarkable system to develop impeccable web applications and sites.
WordPress blogs are highly popular among global viewers, which itself makes it a people™s platform. It is highly intuitive, flexible, scalable, and most significantly an open source Content Management System. Its improved and extended functionality has made it widely acceptable among global developers as their primary web development platform. In the case of development, more is always less, so the core community of WordPress introduced a set of code components to extend the functionality of existing WordPress platform.
WordPress Plugin – A brief Introduction
According To WordPress Codex: A WordPress Plugin is a program, or a set of one or more functions, written in the PHP scripting language, that adds a specific set of features or services to the WordPress weblog, which can be seamlessly integrated with the weblog using access points and methods provided by the WordPress Plugin Application Program Interface (API).
A WordPress Plugin in simple words is a PHP script that leverages and enhances the functionality of a WordPress website. Adding these plugins, you can make your WordPress website work more than just a blogging platform.
For example, you can use membership plugin to build a membership-based site on WordPress. Another example is of an E-commerce plugin which lets you develop an E-commerce website in a simpler way.
Major Highlights Of WordPress Plugin Development
1. Naming Your Plugin
Designing your own plugin should start right from the step of nomenclature. To make sure that your plugin doesn™t match to an existing one, always give a name to it. Do not forget to cross check this name in the WordPress Plugin repository. The trick is to create a name that defines your plugin in few letters. This helps viewers create a perception about your plugin by reading its name only.
2. Plugin Files/Folders
Keep your plugins in a specified folder to avoid any confusion. Generally, all the plugins are saved under wp-content/plugins/ after installation. Every plugin has its own PHP file which is named after the plugin.
For example, if you have a plugin named XYZ-plug, then your PHP file would be like XYZ-plug.php.
Moreover, you can split up your plugin into multiple files. Images, CSS, JavaScript, etc. are some common attributes of these plugin files.
3. Readme File
Readme files are helpful for both developers and users who will visit your plugin folder. These ˜Readme files™ give a brief description of the plugin and sometimes describe the maintenance and new updates to the users.
4. Home Page
Setting your goals is never a bad option. As if you have a plan to share your plugin with the core community of WordPress, wouldn™t it be wise to have a customized homepage. This page can be used later to giveaway an option of download, report and update for the users.
Steps By Step Guide To Create A WordPress Plugin
Below mentioned is a step by step procedure of developing a WordPress Plugin right from scratch. Follow these steps carefully to create the very first plugin on your own. Here they go:
Step 1:
Let™s assume that you are creating a new WordPress plugin, with a name My Plugin.
In the root directory (wp-content/plugins/myplugin) of your plugin, create a file named myplugin.php. Inside this file, insert the WordPress standard comment to recognize this plugin.
An Example For Naming your Plugin
First of all, open your Plugin™s main file and paste in the following code.
WordPress hooks are, mainly, triggers of sorts that allow users to, with short snippets of code, modify areas in a WordPress theme or plugin, or add their own code to various parts of WordPress without modifying the original files.
Action
An Action in WordPress is a hook that is triggered at specific time when WordPress is running which lets you take an action.
After the function is defined, the next step is to “hook” or register it with WordPress. To do this, call add_action() in the global execution space of your plugin file:
[php]
/** Define your plugin class */
Class WP_MY_PLUGINS {
/** Refers to a single instance of this class. */
private static $instance = null;
/**
* Creates or returns an instance of this class.
*
* @return WP_MY_PLUGINS_Theme_Options A single instance of this class.
*/
public static function get_instance() {
if (null == self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
// end get_instance;
/**
* Initialize the plugin by setting localization, filters, and administration functions.
*/
/* ——————————————–*
* Constructor
* ——————————————– */
private function __construct() {
// Add the page to the admin menu.
add_action(‘admin_menu’, array(&$this, ‘include_plugin_menu_page’));
// Register javascript.
add_action(‘admin_enqueue_scripts’, array(&$this, ‘enqueue_admin_js’));
// Add CSS rule.
add_action(‘admin_enqueue_scripts’, array(&$this, ‘add_stylesheet’));
}
/* ——————————————–*
* Functions
* ——————————————– */
/**
* Function will add option page under Appearance Menu.
*/
public function include_plugin_menu_page() {
add_menu_page(‘My plugin’, ‘My Plugin’, ‘manage_options’, ‘my_plugin_page’, array($this, ‘function_1’));
}
//Function that will display the options page.
public function function_1() {
// Perform your code
$name=””;
if(isset($_POST[‘name’])){
$name=$_POST[‘name’];
}else{
$name=get_option( “my_plugin_name” );
}
update_option(“my_plugin_name”,$name); ?>
<!–?php } //Call three JavaScript library (jquery, media-upload and thickbox) and one CSS for thickbox in the admin head. public function enqueue_admin_js() { wp_enqueue_script(‘script’, plugins_url(‘assets/js/upload.js’, __FILE__), array(‘jquery’), ”, true); //It will initialize the parameters needed to show the window properly. } //Function that will add stylesheet file. public function add_stylesheet() { wp_enqueue_style(‘stylesheet’, plugins_url(‘assets/css/stylesheet.css’, __FILE__)); } } // End class STEP 3: WP_MY_PLUGINS::get_instance(); [/php] Now, we are done with the steps, here are few explanations which you must read for better understanding. These points will give you a glimpse to the terminologies generally used in Plugin development. Add Your Plugin Admin Menu We have used the add_menu_page() to create a top-level menu entry. This function involves a number of attributes mentioned below:
- Page title “
Used in the title tag of the page (shown in the browser bar) when it is displayed. - Menu title “
Used in the menu on the left. - Capability “
The user level allowed to access the page. - Menu slug “
The slug used for the page in the URL. - Function “
The name of the function you will be using to output the content of the page. - Icon “
A URL to an image or a Dashicons string. - Position “
The position of your item within the whole menu.
Conclusion:
We are now done with the process of developing the WordPress plugin: Here are some highlights of what we have done above.
Check if you have missed any: We have defined the way to store the plugin files We clearly defined the information header in order to make our plugin visible for WordPress We briefed about the action & hooks and their use We have defined the parameters needed by the site administrator for configuration We added an action hook for when the menu is displayed in the administration panel to help us add a new sub-menu item for our plugin We have added a new sub-menu item to the Settings menu that will link to our plugin’s configuration page We have built the form containing the user inputs for each of the configurable data bits (function_1) Suggestions Regarding Plugin Development The above-mentioned pointers were just a glimpse to the vast programming of creating unique WordPress plugins.
It™s up to you and your requirement if you want to add more functionalities to that plugin. Though WordPress is an intuitive platform but still creating a plugin is not that easy. Effective planning & programming should be done to get an efficient plugin. Here are few suggestions from our end, which might help you to get the exact plugin that you want:
1. Make sure you adhere to the guidelines of WordPress Codex while writing code of a WordPress Plugin. Considering the Inline Documentation Standards will be helpful.
2. Always use unique names to address your plugin, which is totally different from the functions, plugins, themes that already exist in the WordPress Core. So make that process simple, it is better to use a unique function name prefix on all of your Plugin’s functions. Also, define your plugin functions specifically inside a class.
3. It is advised not to hardcode the WordPress database table prefix (usually “wp_”) into your Plugins. Instead make sure you use the $wpdb->prefix variable.
4. Reading a database is inexpensive but documenting it is quite difficult. Databases are a real source of fetching data; therefore, making changes to the database is both complex and expensive. So, try to write less in the database. It is better to prepare the code first which can be followed by writing needed operations.
5. Wherever possible, use WordPress APIs instead of using direct SQL.
For example,
[php] use get_posts() or new WP_Query() instead of SELECT * FROM {$wpdb->prefix}_posts.[/php]
6. If possible, better use the existing database tables instead of creating new custom tables. Think twice before adding a table to avoid complexity in your plugin. This is what most of the programmers do! Most existing databases can be acquired from custom post types and meta data, custom taxonomy, and other standard tables. A standard table provides better user interface and other functionalities at no cost.
7. Pick data as per your requirement only. This is done to reduce the load on database by selecting the data only that you need. If you have to count the number of rows in a table, do not SELECT * FROM as it would select all the data in all the rows which would just be a waste to the memory. Similarly, if you want to add the post_id and the post_author in your Plugin, then you should SELECT only the specific fields. This too will help in minimizing the database load. Always remember, there are hundreds of processes running in the database simultaneously.
8. Reduce PHP errors in your plugin. Add define(‘WP_DEBUG’, true); to your wp-config.php file, and make sure you activate all of your plugins once to see their functionality and check for any errors. And, if you witness any, fix it then and there only.
9. Try not to echo <script> and<br>