Tuesday 16 July 2013

WordPress Plugin Development





WordPress is a free CMS (Content Management System) and open source blogging tool; it has totally changed the look on the internet, whether we are talking about software or website development.

Now in this part we will be discussing how to create a wordpress plugin with our simple but effective steps. when we talk about plugins, then most probably the non tech guys will think of a complex function, followed by the knowledge of lot of tech languages which is just a myth and this is what we will show here, that how easy it is to create a WordPress plugin, all you need to know is a bit of PHP and a basic understanding of WP file structure and Admin panel.

Now before starting up with how to create a WP plugin, We would first like to discuss why to develop a wordpress plugin.

Why to have a WordPress plugin?

In simple words if we want to define plugins, then we can say that these are same as addons added to your browser for making certain things simpler for you.in the same way Plugins are added to Wordpress for making things easy and simple.

Example:

All in one SEO plugin from WordPress is a plugin that help you in optimizing your site and making it search engine friendly with it simple but effective features at a single place, some of the features provided by. All in one SEO plugin are:

  • Google Analytic support
  • Support for Custom Post Types
  • Advanced Canonical URLs
  • Fine tune Page Navigational Links
  • Built-in API so other plugins/themes can access and extend functionality
  • ONLY plugin to provide SEO Integration for WP e-Commerce sites
  • Nonce Security
  • Support for CMS-style WordPress installations
  • Automatically optimizes your titles for search engines
  • Generates META tags automatically
  • Avoids the typical duplicate content found on WordPress blogs
  • For beginners, you don’t even have to look at the options, it works out-of-the-box. Just install.
  • For advanced users, you can fine-tune everything
  • You can override any title and set any META description and any META keywords you want.
  • Backward-Compatibility with many other plugins, like Auto Meta, Ultimate Tag Warrior and others.

Now this is what a simple Plugin can offer you.

And developing a pluging will give you a better understanding as how things work at the backend and another benefit is that being a developer you can have a complete control over the function of your plugin.

wordpress customization

Steps of developing a WP plugin:

1. WP Folder Structure

The wordpress folder structure will show you the basic application directory within wordpress, in here you will find a Plugin directory where you can find all the plugins installed,

For smaller plug-ins which only requires a single .php file, you can place them directly into the plug-ins/ directory. But when you start creating more complex applications, it is a better option to create a sub directory named after your plug-in where you can assemble all JavaScript, CSS, and HTML along with the PHP functions.

wordpress customization
Figure 1
wordpress customization
Figure 2

And if you want to provide your plugin to others for download, so that others to get benefited from it, then these are the simple things that you need to keep in mind before providing a readme.txt file, this file should contain your name and functionality of the plugin and further if you want, that people should know about the updates in the plugins then you should provide details about each revision.

2. Starting Your PHP File

Start with a simple PHP file while creating a new plugin. This should be your plug-in’s official name. For example I have created base code and have named the file quick-subscribe-now.php.
The first lines of your plug-in must be comment. This is extremely important as WordPress will be unable to process your file without it. Below is an example code snippet you can copy and mold towards your own.

wordpress customization


3. WordPress Naming Conventions And Best Practices

While writing code it’s best to follow regulations and guides set up by WordPress. Since there are so many functions defined already, you can avoid duplicity by providing prefix label to all your variables and function names.
  1. <?php  
  2. function Subscribe_Widget() {
  3.                  $widget_ops = array( ‘classname’ => ‘subscribe’, ‘description’ => __(‘An example widget that displays a quick subscribe now form.’, ‘subscribe’) );
  4.                  $control_ops = array( ‘width’ => 300, ‘height’ => 350, ‘id_base’ => ‘subscribe-widget’ );
  5.                  $this->WP_Widget( ‘subscribe-widget’, __(‘Subscribe Widget’, ‘subscribe’), $widget_ops, $control_ops );
  6.      }// the code
  7. }    
  8. ?>  

In the above examples we prefixed all our setting names with quick-subscribe-now. This can be replaced with any keyword of your choosing usually related to your plugin name.

4. Filters and Actions

Filters and actions are two completely different concepts which relate deeply in the ways they manipulate plugin data.

Understanding Add_filter()

filter is used on a bit of text or data being passed into WordPress. With filters you are quite literally able to filter content through your own custom written functions to change data in any way.

Understanding Add_action()

Actions are similar to filters in that they don’t work on bits of data but instead target pre-defined areas in your templates and admin panel.

Example you can apply an action whenever you update or edit a page’s content. A small list of example actions:

  • publish_post – called when a post is published or when status is changed into “published”
  • wp_head – called when the template is loaded and runs the wp_head() function
  • trackback_post – called whenever a new trackback is added into a post

Installing And Running The Plugin

I’ve created a sample file for the plugin to demo if you’d like to skip the hard coding. Simply download this file (rename it to .php) or copy/paste the code into a new PHP document and upload this to your /wp-content/plugins directory.

wordpress customization
wordpress customization

Once completed you’ll want to access the WordPress administration panel and browse your current set of plug-ins for the demo just installed. Once you activate nothing new will happen, not until we manually add in our function call. To do this simply navigates Appearance -> Editor and look forsingle.php.

These are some of the basics to get you started working within WordPress Plugin development. 

Referred By : Headway Web Solution

No comments:

Post a Comment