Thursday, May 26, 2016

Creating Wordpress Plugin From Scratch

Plugins are some PHP scripts that has capability to alter your website – basically, bits or even lots!. You can install and enable them with few clicks.


The good thing about plugins are that they allow you to add features to your site and remain intact, even if you switch themes or upgrade your WordPress install. In Wordpress a plugin is a safe way to try out new things and if you need to implement cross-theme functions.

This post will help you understand how you can develop a plugin for wordpress from scratch. Lets develop a very simple plugin which add facebook OS meta tags for easier way to understand how plugin can be developed. Before developing a plugin for Wordpress I recommend to install Wordpress, and understand folder structure. wordpress provides us function hooks, using which we can add. alter, remove components to the Wordpress site. Let’s look at a real world analogue for hooks. You know those little diaries where the first sentence says: I am the diary of _________. The empty line is where you put your actual name. Once can write any petname, proffesional name, display name etc as per their requirement.

Lets create our plugin step by step.

1.) Create your plugin folder fb-uk-share-tag under wp-content/plugins/ directory fo your Wordpress site.  Keep in mind that whatever you name your plugin’s folder will be your plugin’s slug. A plugin slug should be unique throughout the WordPress Plugin Repository if you want to upload it and make it publicly available. Your plugin name can be different than slug.

2.) Create a new file inside and name it as fb-uk-share-tag.php and write following code. This will be your main plugin file and its name should be the same as your plugin’s slug.

/**
 * Plugin Name: UK Facebook Tags
 * Plugin URI: 
http://php-talk.blogspot.in
* Description: This plugin adds some Facebook Open Graph tags to our single posts.
 * Version: 1.0.0
 * Author: Uttam Kumar
 * Author URI: http://php-talk.blogspot.in
 * License: GPL2
 */
?>
 This code is a PHP comment, which won’t be visible directly in the WordPress admin. WordPress does use the data within it to output the plugin’s name and some other data in the Plugins section of the backend.  You can change information for your plug in. Now go to your wordpress admin area and check for your plugin. Your plugin should be visible.


Plug in data to display in admin panel has been created. meny plug in may require some setting based on which plug in will react and add,update,remove content to the site. Lets create some setting for the plugin.
 For learning purpose, We want admin to have feature to activate or deactivate facebook os share tag.

Lets add following code to  fb-uk-share-tag.php file.

function ukft_admin_actions() {
    add_options_page("UK Facebook OS Tags Setting", "UK Facebook Tag Setting", 1, "ukfbook-tags-setting", "fb_tag_setting");
}
function fb_tag_setting(){
    //include_once('tags_setting.php');
}
add_action('admin_menu', 'ukft_admin_actions');
add_option( 'ukfbtag_enable_os_tag', 1,    NULL, 'yes' );

we are using add_action('admin_menu', 'ukft_admin_actions'); hook to add a submenu under setting in admin menu. While creating admin menu this hook will execute the function ukft_admin_actions. function  ukft_admin_actions  calls add_options_page("UK Facebook OS Tags Setting", "UK Facebook Tag Setting", 1, "ukfbook-tags-setting", "fb_tag_setting"); which add a submenu to the setting menu of admin panel menu list.

We use add_options_page() to create a top-level menu entry. This function takes a number of arguments:
  1. Page title – used in the title tag of the page (shown in the browser bar) when it is displayed.
  2. Menu title – used in the menu on the left.
  3. Capability – the user level allowed to access the page.
  4. Menu slug – the slug used for the page in the URL.
  5. Function – the name of the function you will be using to output the content of the page.
 Notice that funtion fb_tag_setting() Contains include_once which is commented. Now after you activate your plugin, You can see UK Facebook Tag Setting  under setting menu. If you click on the this menu you'll find a blank page because in the function fb_tag_setting()  nothing is written to render the page.

3) Create another file named tags_setting.php. Open the file and write the code below.



 This code will render a small form asking administrator about Enable Facebook OS Tag. Once form is submitted, it  update the setting options. Notice the code $fb_os_tag=get_option('ukfbtag_enable_os_tag'); and  update_option('ukfbtag_enable_os_tag', $_POST['fb_os_tag']); . get_option() function is used to get setting option and update_option() function is used to update setting option added prior for our plugin setting. In our case, we have added our option in fb-uk-share-tag.php. see the code add_option( 'ukfbtag_enable_os_tag', 1,    NULL, 'yes' );. It has  4 parameters explained below 
  1. Option Name – variable name of the setting option.
  2. Value – default value for the setting option
  3. NULL– depricated or not
  4. autoload – to load this setting automatically or not. Value can be 'yes' or 'no'.
our pugin development for admin area is done now. If you click on your plugin link under setting menu. Page to enable or disable facebook share tags will be displayed.



4) Now lets add some code which will add facebook os tag, if it is enabled in our plugin setting area. open file fb-uk-share-tag.php  add the following code.


the hook add_action( 'wp_head', 'uk_facebook_tags' ); will call uk_facebook_tags() function while loading the header of website. this function checks option provided in setting of plugin and if enabled then add facebook os meta tags for each post, if single post is displayed and load rest of the website.

Conclusion

There are ton and tons of things you can do with plugins and almost as many ways you can create them.for the sake of simplicity, I've not put any attention towards folder structure and OOPS based coding. You can have your well defined folder structure and code which follows OOPs and adheres to Wordpress Standard.
Please


 


No comments: