Create WordPress Plugin

[wp_ad_camp_1]

Earlier, WordPress is used only for the blog. But now we can use WordPress for making attracting business, shopping, membership and many more websites. WordPress can be easily customized using themes and plugins. You can read more details about WordPress Theme here. Plugins are used to extend the functionality of a website. You can read more details about WordPress Plugins here. There are number of plugins available in WordPress.

Creating a plugin in WordPress is not a difficult task. If you have knowledge of PHP, you can easily create your own custom plugin. In this lesson, we will show, how we can Create a custom Plugin in WordPress.

Following are steps to create a plugin in WordPress:
 

File & Names

The first thing we have to do is start with creating folder for your plugin. Give a unique name to the folder. Here we are creating a folder named basic-demo. Inside it, create two files named basic-demo.php and readme.txt.

The basic-demo.php is the main file which contains the entire coding and readme.txt file contains information about your plugin.

Open your FTP and copy the folder inside /wp-content/plugins folder located in your WordPress folder.

Plugin Information

Step (1) – Open basic-demo.php file and paste following code:

<?php
/*
Plugin Name: Basic-demo
Plugin URI: http://www.freewptutorial.com
Description: A simple basic demo wordpress plugin
Version: 1.0
Author: freeWptutorial
Author URI: http://www.freewptutorial.com
License: GPL
*/
?>

 
This is the basic information of your plugin. The information is commented, so it is used to show only the information about your plugin in the dashboard. Save this file.

Step (2) -Log in to dashboard and navigate to Plugins >> Installed Plugins.
 

Step (3) – Activate the plugin.
 

 
Step (4) – The plugin will show nothing on the front end. To solve this add following code in the basic-demo.php file:

/* This calls basic_demo() function when wordpress initializes.*/
/* Note that the basic_demo doesnt have brackets.*/
add_action('init','basic_demo');
function basic_demo()
{
echo "basic demo";
}

 
Step (5) – Final code will look like this:

<?php
/*
Plugin Name: Basic-demo
Plugin URI: http://www.freewptutorial.com
Description: A simple basic demo wordpress plugin
Version: 1.0
Author: freeWptutorial
Author URI: http://www.freewptutorial.com
License: GPL
*/
/* This calls basic_demo() function when wordpress initializes.*/
/* Note that the basic_demo doesnt have brackets.*/
add_action('init','basic_demo');
function basic_demo()
{
echo "basic demo";
}
?>

 

Test Plugin

Now its time to call this plugin in your index.php file. We are going to define a function in index.php file to call the plugin.

Step (1) – Open index.php file located in the activated theme folder of your WordPress and paste following code anywhere:

<?php
if(function_exists('basic_demo')) {
basic_demo();
}
?>

 
Step (2) – Save file and visit your website front end to see the changes.
 

Activate/Deactivate plugin

We are going to create some important function which are necessary to activate and deactivate a plugin.

Step (1) – Paste following code in the basic-demo.php file:

<?php
/* Runs when plugin is activated */
register_activation_hook(__FILE__,'basic_demo_install'); 

/* Runs on plugin deactivation*/
register_deactivation_hook( __FILE__, 'basic_demo_remove' );

function basic_demo_install() {
/* Creates new database field */
add_option("basic_demo_data", 'Default', '', 'yes');
}

function basic_demo_remove() {
/* Deletes the database field */
delete_option('basic_demo_data');
}

?>

 

Setting Option page

Step (1) – Paste following code in the basic-demo.php file:

<?php
if ( is_admin() ){

/* Call the html code */
add_action('admin_menu', 'basic_demo_admin_menu');

function basic_demo_admin_menu() {
add_options_page('basic demo', 'basic demo', 'administrator',
'basic-demo', 'basic_demo_html_page');
}
}
?>

Step (2) – Go to your dashboard and navigate to Settings >> basic demo. You can see setting page is there.
 

 
Step (3) – Finally add the following code to basic-demo.php file:

<?php
function basic_demo_html_page() {
?>
<div>
<h2>Basic Demo Options</h2>

<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>

<table width="510">
<tr valign="top">
<th width="92" scope="row">Enter Text</th>
<td width="406">
<input name="basic_demo_data" type="text" id="basic_demo_data"
value="<?php echo get_option('basic_demo_data'); ?>" />
(ex. Basic Demo)</td>
</tr>
</table>

<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="basic_demo_data" />

<p>
<input type="submit" value="<?php _e('Save Changes') ?>" />
</p>

</form>
</div>
<?php
}
?>

 
This is the basic form of our plugin. You can enter text in the setting page and it will display on the front end.

[wp_ad_camp_3]
Spread the love