Sunday, 17 June 2012

Introduction to Wordpress Plugin Development

—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

—Now a days many plugins are available and for requirement first need to search for available functions if it is already available you can use them directly else need to develop.

—In development of plugin first part of the page required to be comment describing about the plugin, author, license, etc.

—It all should be written within the php comment.

<?php

/*

Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates Description: description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2

*/

?>

WordPress Plugin Hooks


Many WordPress Plugins accomplish their goals by connecting to one or more WordPress Plugin "hooks". 


The way Plugin hooks work is that at various times while WordPress is running, WordPress checks to see if any Plugins have registered functions to run at that time, and if so, the functions are run. These functions modify the default behavior of WordPress.

For instance, before WordPress adds the title of a post to browser output, it first checks to see if any Plugin has registered a function for the "filter" hook called "the_title". If so, the title text is passed in turn through each registered function, and the final result is what is printed. So, if your Plugin needs to add some information to the printed title, it can register a "the_title" filter function.

Template Tags

Another way for a WordPress Plugin to add functionality to WordPress is by creating custom Template Tags. Someone who wants to use your Plugin can add these "tags" to their theme, in the sidebar, post content section, or wherever it is appropriate.
In a theme file functions are written with the help of php code <?php ?>

Saving Plugin Data to the Database

Plugins will need to get some input from the site owner or blog users and save it between sessions, for use in its filter functions, action functions, and template functions. This information has to be saved in the WordPress database, in order to be persistent between sessions.


There are three methods for saving Plugin data in the database:

÷   Use the WordPress "option" mechanism (described below). This method is appropriate for storing         relatively small amounts of relatively static, named pieces of data -- the type of data you'd expect the site owner to enter when first setting up the Plugin, and rarely change thereafter.

÷ Post Meta : Appropriate for data associated with individual posts, pages, or attachments ex. add_post_meta(), update_post_meta()

÷ Create a new, custom database table : This method is appropriate for data not associated with individual posts, pages, attachments, or comments -- the type of data that will grow as time goes on, and that doesn't have individual names.
There are two kind of Hooks available in wordpress.


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.

Actions

—Actions are triggered by specific events that take place in WordPress, such as publishing a post, changing themes, or displaying a page of the admin panel. Your plugin can respond to the event by executing a PHP function, which might do one or more of the following:

÷   Modify database data
÷   Send an email message
÷   Modify what is displayed in the browser screen (admin or end-user)

Ex.

class emailer {
function send($post_ID) {
$friends = 'bob@example.org,susie@example.org';
mail($friends,"sally's blog updated",'I just put something on my blog: ');
return $post_ID;
}
}

add_action('publish_post', array('emailer', 'send'));


Actions Functions
÷   has_action()
÷    add_action()
÷    do_action()
÷    do_action_ref_array()
÷    did_action()
÷    remove_action()
÷    remove_all_actions()

After your 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:

add_action ( 'hook_name', 'your_function_name', [priority], [accepted_args] );

÷    hook_name - name of an action hook provided by WordPress, that tells what event your function should be associated with.
÷ your_function_name  - The name of the function that you want to be executed following the event specified by hook_name. This can be a standard php function, a function present in the WordPress core, or a function defined by you in the plugin file (such as 'email_friends'defined above).
÷ priority - An optional integer argument that can be used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
÷ accepted_args An optional integer argument defining how many arguments your function can accept (default 1), useful because some hooks can pass more than one argument to your function.

Ex. add_action ( 'publish_post', 'email_friends' );

  ÷ hook_name
                      init - Typically used by plugins to initialize
                      register_sidebar – for each sidebar
                      wp_footer – for footer
                      Shutdown – php execution about to end / end of action
                      loop_start – loop start
                      Loop_end – loop end (ex. In comment)
                      plugins_loaded – After active plugins and pluggable functions are loaded
                      wp_loaded – when full wordpress is loaded

                 Visit for more hook_name http://codex.wordpress.org/Plugin_API/Action_Reference

has_action() 
Desc : Check if any action has been registered for a hook. 
<?php has_action( $tag, $function_to_check ) ?> 
÷  $tag(string) (required) The name of the action hook.
         Default: None
÷  $function_to_check(callback) (optional) If specified, return the priority of that function on this hook or false if not attached.Default: fals 

do_action()
       Desc : Executes a hook created by add_action. Placing argument for add_action.
<?php do_action( $tag, $arg ); ?> 
÷  $tag(string) (required) The name of the hook you wish to execute.
Default: None 
÷   $arg(mixed) (optional) The list of arguments to send to this hook. 
Default: ''

do_action_ref_array()
Desc : Execute functions hooked on a specific action hook, specifying arguments in an array. 
This function is identical to do_action, but the arguments passed to the functions hooked to $tag are supplied using an array. 
<?php do_action_ref_array( $tag, $arg ); ?>  
÷  $tag(string) (required) The name of the action to be executed.
         Default: None 
÷  $args(array) (required) The arguments supplied to the functions hooked to $tag
Default: None
did_action()
Desc : Retrieve the number of times an action is fired.
—<?php did_action( $tag ); ?> 
remove_action()
Desc : This function removes a function attached to a specified action hook. This method can be used to remove default functions attached to a specific action hook and possibly replace them with a substitute. 
<?php remove_action( $tag, $function_to_remove, $priority, $accepted_args ); ?>
remove_all_actions()
Desc : Remove all of the hooks from an action. 
<?php remove_all_actions( $tag, $priority ) ?> 

Filters

 Filters are functions that WordPress passes data through, at certain points in execution, just before taking some action with the data.

Filters sit between the database and the browser - when WordPress is generating pages.
and between the browser and the database - when WordPress is adding new posts and comments to the database

The basic steps to adding your own filters to WordPress (described in more detail below) are:

÷  Create the PHP function that filters the data.
÷  Hook to the filter in WordPress, by calling add_filter()
÷  Put your PHP function in a plugin file, and activate it.

Create a Filter Function
A filter function takes as input the unmodified data, and returns modified data (or in some cases, a null value to indicate the data should be deleted or disregarded). If the data is not modified by your filter, then the original data must be returned so that subsequent plugins can continue to modify the value if necessary. 
function filter_profanity($content) {
    $profanities = array('badword','alsobad','...');
    $content=str_ireplace($profanities,'{censored}',$content);
    return $content;
add_filter('comment_text','filter_profanity');
add_filter()
Desc : Hooks a function to a specific filter action. 
<?php add_filter( $tag, $function_to_add, $priority, $accepted_args ); ?> 
has_filter()
Desc : Check if any filter has been registered for a hook. 
<?php has_filter( $tag, $function_to_check ); ?>
apply_filter()
Desc : Call the functions added to a filter hook. 
<?php apply_filters( $tag, $value, $var ... ); ?> 
current_filter()
Desc : Retrieve the name of the current filter or action. 
<?php current_filter(); ?>