articles

Home / DeveloperSection / Articles / Creating Plugin in WordPress

Creating Plugin in WordPress

Anonymous User 8561 15-Nov-2011

WordPress Plugins allow easy modification, customization, and enhancement to a WordPress blog. Instead of changing the core programming of WordPress, you can add functionality with WordPress Plugins.

“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”.

To create a plugin using WordPress see the following step:

Step 1: Creating Plugin Name

Step 2: Creating Plugin Files

Step 3: Installing Plugin in to WordPress (Website)

Create Plugin Name:

The first task in creating a WordPress Plugin is to think about what the Plugin will do, and make a unique name for your Plugin. Check out Plugins and the other repositories it refers to, to verify that your name is unique. Here I am creating plugin with ‘MyTestingPlugin’ name.

Note: Here I am giving a blank plugin demonstration.

Plugin File:

The next step is to create a PHP file with a name derived from your chosen Plugin name. For instance, if you choose plugin name such as ‘MyTestingPlugin’ then you might call your PHP function file ‘’MyTestingPlugin.php”, It is not mandatory but used for uniqueness of file name.

People who install your Plugin will be putting this PHP file into the WordPress Plugin directory (wp-content/plugins/) in their installation, so no two Plugins they are using can have the same PHP file name.

Your WordPress Plugin must have at least one PHP file; it could also contain JavaScript files, CSS files, image files, language files, etc.  I would like to tell you basically two files are used to create a plugin such as PHP main file (Home Page file) and readme file. Let’s see the brief description on them;

Home Page:

It is also very useful to create a web page to act as the home page for your WordPress Plugin. This page should describe how to install the Plugin, what it does, what versions of WordPress it is compatible with, what has changed from version to version of your Plugin, and how to use the Plugin.

Home page has three sections ‘Page header’, ‘Copyright Section’ and ‘Programming Section’. Let’s see a brief knowledge of these.

Page Header:

Page header section described the brief knowledge of plugin such as author, description, plugin name, plugin URI etc.

Example:

/*

 Plugin Name: MyTestingPlugin
 Plugin URI: http://www.xyz.com
 Description: The MyTestingPlugin is an empty or sample plugin to help you get started on writing a WordPress plugin.
Version: 1.1
 Author: Arun Singh
Author URI: http://www.xyz.com
*/


 Copyright Section:

It is customary to follow the standard header with information about licensing for the Plugin. Here I am not mention any copyright content but you can. 

/* Copyright 2011 Arun Singh

       -------------- Write your copyright content here-------------------------
    Write some copyright content
*/

 Programming Section:

Now, it's time to make your Plugin actually do something. This section contains some general ideas about Plugin development, and describes how to accomplish several tasks your Plugin will need to do.

Code of MyTestingPlugin.php file: 
<?php

/*
Plugin Name: MyTestingPlugin
Plugin URI: http://www.xyz.com
Description: The MyTestingPlugin is an empty or sample plugin to help you get started on writing a WordPress plugin.
Version: 1.1
Author: Arun Singh
Author URI: http://www.xyz.com
*/
/* Copyright 2011 Arun Singh
   Write some copyright content
*/
// don't load directly
if (!function_exists('is_admin')) {
    header('Status: 403 Forbidden');
    header('HTTP/1.1 403 Forbidden');
    exit();
}
// Pre Compatibility
if ( ! defined( 'WP_CONTENT_URL' ) )
      define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
if ( ! defined( 'WP_CONTENT_DIR' ) )
      define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
define( 'plugin_example_DIR', WP_PLUGIN_DIR . '/MyTestingPlugin-plugin' );
define( 'plugin_example_URL', WP_PLUGIN_URL . '/MyTestingPlugin-plugin' );
if (!class_exists("plugin_example")) :
class plugin_example {
                var $addpage;
                function plugin_example() {
                                add_action('admin_init', array(&$this,'init_admin') );
                                add_action('init', array(&$this,'init') );
                                add_action('admin_menu', array(&$this,'add_pages') );
                                register_activation_hook( __FILE__, array(&$this,'activate') );
                                register_deactivation_hook( __FILE__, array(&$this,'deactivate') );
                }

                function activate() {
                                global $wpdb;
                                if (function_exists('is_multisite') && is_multisite()) {
                                                // check if it is a network activation - if so, run the activation function for each blog id
                                                if (isset($_GET['networkwide']) && ($_GET['networkwide'] == 1)) {
                                                                $old_blog = $wpdb->blogid;
                                                                // Get all blog ids
                                                                $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs"));
                                                                foreach ($blogids as $blog_id) {
                                                                                switch_to_blog($blog_id);
                                                                                $this->_activate();
                                                                }
                                                                switch_to_blog($old_blog);
                                                                return;
                                                }
                                }
                                $this->_activate();
                }
                function deactivate() {
                                global $wpdb;
                                if (function_exists('is_multisite') && is_multisite()) {
                                                // check if it is a network activation - if so, run the activation function for each blog id
                                                if (isset($_GET['networkwide']) && ($_GET['networkwide'] == 1)) {
                                                                $old_blog = $wpdb->blogid;
                                                                // Get all blog ids
                                                                $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs"));
                                                                foreach ($blogids as $blog_id) {
                                                                                switch_to_blog($blog_id);
                                                                                $this->_deactivate();
                                                                }
                                                                switch_to_blog($old_blog);
                                                                return;
                                                }                                 }
                                $this->_deactivate();
                }
                function _activate() {}
                function _deactivate() {}
                function init_admin() {
                }
// function to internationalization of plugin
                function init() {
                                load_plugin_textdomain( 'plugin_example', plugin_example_DIR . '/lang', basename( dirname( __FILE__ ) ) . '/lang' );
                }
                function add_pages() {
                                // Add a new submenu
                                $this->addpage = add_options_page( __('MyTestingPlugin', 'plugin_example'), __('MyTestingPlugin', 'plugin_example'),                                                                                                                                                           'administrator', 'plugin_example',                                                                                                                                                                array(&$this,'add_plugin_example_page') );                                 add_action("admin_head-$this->addpage", array(&$this,'add_plugin_example_admin_head'));
                                add_action("load-$this->addpage", array(&$this, 'on_load_plugin_example_page'));
                                add_action("admin_print_styles-$this->addpage", array(&$this,'add_plugin_example_admin_styles'));                                 add_action("admin_print_scripts-$this->addpage", array(&$this,'add_plugin_example_admin_scripts'));
                }
                function add_plugin_example_admin_head() {
                }
                function add_plugin_example_admin_styles() {
                }
                function add_plugin_example_admin_scripts() {
                }
                function on_load_plugin_example_page() {
                }
                function add_plugin_example_page() {
                                include('MyTestingPlugin-page.php');
                }
                function print_example($str, $print_info=TRUE) {
                                if (!$print_info) return;
                                __($str . "<br/><br/>\n", 'plugin_example' );
                }
} // end class
endif;
global $plugin_example;
if (class_exists("plugin_example") && !$plugin_example) {
    $plugin_example = new plugin_example();
}
?>

 Code of MyTestingPlugin-page.php File:
<?php

if (!function_exists('is_admin')) {
    header('Status: 403 Forbidden');
    header('HTTP/1.1 403 Forbidden');
    exit();
}
global $plugin_example;
$action = '';
$location = "options-general.php?page=plugin_example"; // based on the location of your sub-menu page
switch($action) :
default:
endswitch;
if ($action) {
                // clear $_POST array if needed
                // redirect after header definitions cannot use wp_redirect($location);
              ?>
                <script type="text/javascript">
                <!--
                window.location='<?php echo $location; ?>';
                //-->
                </script>
                <?php
                exit;
}
$messages[1] = __('Example message 1.', 'plugin_example');
if ( isset($_GET['message']) && (int) $_GET['message'] ) {
                $message = $messages[$_GET['message']];
                $_SERVER['REQUEST_URI'] = remove_query_arg(array('message'), $_SERVER['REQUEST_URI']);
}
$title = __('MyPlugin', 'plugin_example');
?>
    <div class="wrap">
    <?php screen_icon(); ?>
    <h2><?php echo esc_html( $title ); ?></h2>
                <?php                                 if ( !empty($message) ) :
                                ?>
                                <div id="message" class="updated fade"><p><?php echo $message; ?></p></div>
                              <?php
                                endif;
                ?>
</div>

 Readme File:

If you want to host your Plugin on http://wordpress.org/extend/plugins/, you also need to create a readme.txt file in a standard format, and include it with your Plugin.

Note: WordPress plugin repository takes the "Requires" and "Tested up to" versions from the readme.txt in the stable tag. Let’s see the structure of readme file.

Code of ‘reame.txt’ file: 
=== MyTestingPlugin ===
Contributors: Arun Singh
Donate link: http://xyz.com
Tags: example plugin, wordpress example plugin, write plugin, sample plugin, empty plugin, test plugin
Requires at least: 2.9
Tested up to: 2.9
Stable tag: 1.0
The MyTestingPlugin Example is an empty or sample plugin to help you get started on writing a WordPress plugin.
== Description ==
<strong>The MyTestingPlugin Example Plugin contains</strong> -
<ul>
<li>A plugin activation hook that runs when a plugin is first activated.</li>
<li>Plugin initialization functions.</li>
<li>An example plugin sub-menu page.</li>
<li>An uninstall.php file that runs when your plugin is uninstalled. Use this to clear out options (<a href="http://codex.wordpress.org/Function_Reference/delete_option"><em>delete_option</em></a>) or databases created by your plugin.</li> <li>Example localization or language translation hooks. This enables others to easily add language translation capabilities to your plugin.</li> <li>A plugin class wrapper which wraps around your plugin functions so that there will not be any function name conflicts with other plugins or themes in the user's environment.</li> </ul>
== Installation ==
1. Upload `MyTestingPlugin.zip` onto your local computer.
2. Go to your WordPress Dashboard and select <strong>Plugins >> Add New</strong>.
3. Click on the <strong>Upload</strong> option at the top and select the `MyTestingPlugin.zip file` you just downloaded.
4. Click on <strong>Install</strong>.
5. Activate the plugin through the 'Plugins' menu in WordPress
6. There should be an additional MyTestingPlugin Example sub-menu under the Settings option of your Dashboard.
7. Clicking on the MyTestingPlugin Example sub-menu will cause MyTestingPlugin-page.php to be executed.
== Frequently Asked Questions ==
== Screenshots ==
== Changelog ==

 Installing Plugin:

After successfully creating readme and home page file put them into a directory and zipped it, now upload this zipped file into WordPress (Website).

Login in to WordPress Administrator Dashboard panel and open Administrator (Dashboard Panel) -> Plugins-> Add New-> Upload

Creating Plugin in WordPress

After browsing the plugin zip file click on ‘Install Now’ button.

Creating Plugin in WordPress

After successfully activating the plugin, it will be visible in ‘Settings’ menu option where you can change the default setting of Plugin.

Creating Plugin in WordPress

Now your Plugin successfully activated and you can use it.


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By