plug in development

20
WordPress Plugin Development

Upload: lucky-ali

Post on 18-May-2015

120 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Plug in development

WordPress

Plugin Development

Page 2: Plug in development

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

Page 3: Plug in development

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.

Page 4: Plug in development

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_WorldPlugin URI: www.HellowWorld.com/Description: A simple hello world wordpress pluginVersion: 1.0Author: AliShahAuthor URI: [email protected]: GPL

*/?>

Activate Your Plugin.

Page 5: Plug in development

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.

Page 6: Plug in development

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.

Page 7: Plug in development

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

Page 8: Plug in development

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.

Page 9: Plug in development

1. Register_ Activation_Hook

DescriptionThe 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.Exampleregister_activation_hook( __FILE__, ‘Hello_World_Install' );

Page 10: Plug in development

2. Register_ Deactivation _Hook

DescriptionThe 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.Exampleregister_deactivation_hook( __FILE__, ‘Hello_World_Remove’ );

Page 11: Plug in development

Add Option Functions

Add _OptionThis 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’)

Page 12: Plug in development

Delete Option Functions

Delete _OptionThis 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’’) ?>

Page 13: Plug in development

In our example

Page 14: Plug in development

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.

Page 15: Plug in development

Plugin Settings Page

So , first task is to add the Hello_World options in theDashboardSettingsHello 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().’

Page 16: Plug in development

Plugin Settings Page

<?phpif ( 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');}}?>

Page 17: Plug in development

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');}}?>

Page 18: Plug in development

Add_Options_page Function

DescriptionAdd sub menu page to the Settings menu.Syntax<?phpadd_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.Exampleadd_options_page('Hello World', 'Hello World Menu', 'administrator','hello-world', 'hello_world_html_page');

Page 19: Plug in development
Page 20: Plug in development

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.