2. What is a Plugin?
Plugins are tools to extend the functionality of WordPress.
Plugins offer custom functions and features so that each user can tailor their site to
their specific needs.
WordPress is gaining more and more popularity each day, not just as a blogging
platform but also as a basic CMS, thus improving and extending its basic
functionality becoming a day-to-day necessity for a lot of developers. Fortunately,
the WordPress developers have foreseen these needs and added the possibility of
customizing the basic functionality by adding plugins. Basically, a WordPress
plugin is a (more or less) stand-alone piece of code that can be executed in different
sections and stages within a page or site
3. Steps to Create New PlugIn.
We will create a very simple plugin in this presentation. This
example will surely make u understand about the core concept of developing
plugins. Once you have idea of plugins, u can create more plugins by following the
same method and altering the code according to your need. Follow the step by step
procedure given below.
Plugins reside in WordpressWp-ContentPlugins
Step1. Create a Folder in Plugins directory and name it as Hello_World.
Step2. Create a php file with the same name in Hello_World folder.
i.e. Hello_World.php.
4. Step3. Add this code in the start of newly created file.
This code will let the wordpress understand that this is a plugin file. After Adding
this code You can check in DashboardPlugins, here u can see you plugin with all
the specification given in commented code.
<?php
/*
Plugin Name: Hello_World
Plugin URI: www.HellowWorld.com/
Description: A simple hello world wordpress plugin
Version: 1.0
Author: AliShah
Author URI: AliShah_120@yahoo.com
License: GPL
*/
?>
Activate Your Plugin.
5. Step4. Up till Now we have created a plugin, which has been activated
but it performs no functionality. This plugin should do something. So we place a
code which will print HelloWorld. For this purpose we will create a function which
will print hello world. And we will initiate this function with startup of wordpress
site . In this code a function will be used.
<?php
Add_action ('init','Hello_World');
function Hello_World()
{
Echo „ This Plugin is Working Fine‟;
}
?>
Add this code in Hello_World.php plugin file. Add_action is a function which
hooks a function with some action. In our example This calls Hello_World()
function when wordpress initializes.
6. Now you can see that „This Plugin is Working Fine‟ is written
somewhere on your website . If you can see this line. It means you are
doing good uptill now. Our Hello_World.php file has the following code
uptill now.
Our Hello World plugin is nearly done and with just few lines of code.
When our plugin is activated, add_action command calls our
hello_world() function when wordpress starts loading.
Why not, we build a plugin options page in admin area and provide a
backend for plugin users?
Right now, the plugin outputs hello world (its pretty much static) and if
somebody wants to output „„This Plugin is Working Fine‟ , they need to
open the php file and make changes everytime to print different text.
Asking the user to edit plugin files isnt a good idea! As a wordpress
plugin developer, it is you, who has to provide a good wordpress options
interface in the wp-admin area.
7. What should a Plugin do?
Here is what we do….
•When plugin gets activated, we create new database field `wp_hello_world_data`
using set_options() function.
•When plugin gets deactivated, we delete the database field `wp_hello_world_data`
•We create a options menu for Hello World in WordPress Admin > Settings.
•We save the user entered data in the wordpress database.
•We retrieve the data stored in wordpress database and output it using get_options()
function.
•Why we are creating database field? because the saved data must be saved
somewhere? ie. in wordpress database. This way the plugin outputs user entered
text, instead of the static “Hello World”.
8. Hooks
Hooks are provided by WordPress to allow your plugin to 'hook
into' the rest of WordPress; that is, to call functions in your plugin at specific times,
and thereby set your plugin in motion. There are two kinds of hooks:
Actions: Actions are the hooks that the WordPress core launches at specific points
during execution, or when specific events occur. Your plugin can specify that one
or more of its PHP functions are executed at these points, using the Action API.
Filters: Filters are the hooks that WordPress launches to modify text of various
types before adding it to the database or sending it to the browser screen. Your
plugin can specify that one or more of its PHP functions is executed to modify
specific types of text at these times, using the Filter API.
9. 1.
Register_ Activation_Hook
Description
The register_activation_hook function registers a plugin function to be run when
the plugin is activated.
Syntax
<?php register_activation_hook( $file, $function ); ?>
Parameters...
$file: (string) (required) Path to the main plugin file inside the wp-content/plugins
directory. A full path will work. Default: None
$function:
The function to be run when the plugin is activated.
Example
register_activation_hook( __FILE__, „Hello_World_Install' );
10. 2.
Register_ Deactivation _Hook
Description
The function register_deactivation_hook (introduced in WordPress 2.0) registers a
plugin function to be run when the plugin is deactivated.
Syntax
<?php register_deactivation_hook($file, $function); ?>
Parameters...
$file: (string) (required) Path to the main plugin file inside the wp-content/plugins
directory. A full path will work. Default: None
$function:
$function:
The function to be run when the plugin is activated.
Example
register_deactivation_hook( __FILE__, „Hello_World_Remove‟ );
11. Add Option Functions
Add _Option
This function will be used in Hello_World_Install(function). Add_Option is used to
set the user given value to plugin .
Syntax
<?php add_option( $option, $value, $deprecated, $autoload ); ?>
Parameters...
$option: Name of the option to be added. This is going to be placed into the database.
$value: Default value since we explicitly set it.
$deprecated: description. No need to give for versions after 2.0.
$autoload: should this option be automatically loaded when the page load . The value
may be Yes or No.
Example
<?php Add_option(„‟Hello_World_Data‟‟, „Default‟, „‟, „yes‟)
12. Delete Option Functions
Delete _Option
This function will be used in Hello_World_remove(function). A safe way of removing
a named option/value pair from the options database table.
Syntax
<?php delete_option( $option ); ?>
Parameters...
$option: Name of the option to be added. This is going to be deleted.
Example
<?php delete_option(„‟Hello_World_Data‟‟) ?>
14. Plugin Settings Page
This is our final step. All we need to create is plugin
settings page in the wordpress admin area. The settings page will update and
save the data to the database field `hello_world_data` which we created while
activating the plugin.
Here is a very important thing to remember:
The add_action for admin_menu should call a function
hello_world_admin_menu() containing add_options_page, which in turn
should call a function hello_world_html_code() containing html code.
15. Plugin Settings Page
So , first task is to add the Hello_World options in the
DashboardSettingsHello World .
For this purpose we will use the conditional tag for
confirmation of admin.
The tag is
If (is_admin() )
{
Run this code(here we will write the code for showing the
setting page for admin)
}
In this if condition an add_action () function is used which will call a
function „Hello_world_admin_menu().‟
16. Plugin Settings Page
<?php
if ( is_admin() )
{
/* Call the html code */
add_action ('admin_menu', 'hello_world_admin_menu');
function hello_world_admin_menu()
{
add_options_page('Hello World', 'Hello World', 'administrator',
'hello-world', 'hello_world_html_page');
}
}
?>
17. Plugin Settings Page
add_action ('admin_menu', 'hello_world_admin_menu');
Add_action is calling a function named 'hello_world_admin_menu„
in the body of this called function there is another function which is calling
for html page. This call is through an other function
„hello_world_html_page‟.
function hello_world_admin_menu()
{
add_options_page ('Hello World', 'Hello World', 'administrator',
'hello-world', 'hello_world_html_page');
}
}
?>
18. Add_Options_page
Function
Description
Add sub menu page to the Settings menu.
Syntax<?php
add_options_page ( $page_title, $menu_title, $capability, $menu_slug, $function);
?>
Parameters...
$page_title: this is the title of settings page.
$menu_Title: this is the title in dashboardsettings title
$capability: here we can give user who have permissions. i.e Admin
$menu-slug: brief info about menu.
$function: here the name of function. Which is to call.
Example
add_options_page('Hello World', 'Hello World Menu', 'administrator',
'hello-world', 'hello_world_html_page');
19.
20. html Page
This is the last step. In previous slide , on add_options_page function we called a
function name „hello_world_html_page‟. Now we have to this function. We will
create a text filed and button, under the form object. User will write the desired
string and press the update button the value will be saved in data base.