Loading...
You are here:  Home  >  Web Development  >  Current Article

Build Your First WordPress Plugin In Minutes

By   /   March 8, 2013  /   1 Comment

WordPress Plugin Development has never been easy until you learn how to code the simple WordPress Plugin that will show the basic RSS, Twitter, Stats on the Widget area of your blog giving a clean user interface and a stylish CSS layout gives wings to this first WordPress Plugin Development Stage. As Mentioned in the Previous article, the list of must have WordPress plugins, this WordPress Plugin which we are about to create is also an important Plugin or Widget For a Blog to socialize your Content.

DOWNLOAD THE PLUGIN FILE BELOW

Twitter-Rss-WappDev-Counter

Lets Start Developing

STEP 1: Create a Folder in your Machine and name it anything you want, basically the name of your plugin, here in our case, I name this Plugin as WAPPDEV STATS COUNTER, the next step is create two folders into the Plugin folder namely CSS and IMG, these two folders are used to carry your .CSS style sheets and the necessary images needed for the interface. now lets have a short look at the basics of creating a plugin  keeping the WordPress Codex in our mind, as we have to follow these Codex Rules to Successfully create the Plugin The heart of a WordPress plugins is the below 2 functions,

It is very important to know the difference between the above functions.

  • add_action –> does an action at various points of WordPress execution
  • add_filter –> does filtering the data (eg. escaping quotes before mysql insert, or during output to browser.)

Refer to the WordPress Plugin API for more better understanding.

STEP 2: First of all we have to declare that this is plugin file and make WordPress identify it as a Plugin and we can do this by adding the following block of code at the very beginning of the .php file.

STEP 3: The first thing we have to do is load our widget when necessary. WordPress provides the widgets_init action hook that will allow us to do this. This action hook is fired right after the default WordPress widgets have been registered.

we simply have to extend the preexisting WP_Widget class. So, the first step is creating a new class with a unique name.

Then, we’ll want to add our first function. This function will be what makes our widget unique to WordPress, and it’ll allow us to set up the widget settings. Note that the class name and first function name are the same. In this example this is Wappdev_stats.

STEP 4: The next function within our WP_Widget  class will handle the display of the widget. This code might be a little confusing because we don’t know what it all means (we haven’t added the controls).

The goal here is to take the settings supplied by what the user selected for the widget and display the widget according to those values.

It’s also important to make sure you use the  $before_widget, $after_widget, $before_title, and $after_title  variables. These are provided by the theme and should not be hardcoded. How widgets are displayed should always be handled by the theme.

STEP 5:  Most WordPress Widgets 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 two basic methods for saving Widget data in the database.

The $wpdb object can be used to read data from any table in the WordPress database, not just the standard tables that WordPress creates. For example to SELECT some information from a custom table called “mytable”, you can do the following.

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 Widget, and rarely change thereafter. Option values can be strings, arrays, or PHP objects (they will be “serialized”, or converted to a string, before storage, and unserialized when retrieved). Option names are strings, and they must be unique, so that they do not conflict with either WordPress or other Plugins. Here are function you will need to modify options.

This method is appropriate for data 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. See Creating Tables with Plugins for information on how to do this.

STEP 6: The reason the new widget class in WordPress is so cool is how easy it is to set up controls for your widgets. important things The get_field_id() and get_field_name() functions handle most of the dirty work, leaving us to concentrate on more  like actually creating the widget. Take special notice of how these functions are used because it’ll make life much simpler for you.

First, we might want to set up some defaults. By setting up defaults, we can control what’s shown just in case the user doesn’t select anything.

Finally the Whole PHP code is as Follows:

STEP 7: CSS Stylesheets

Now, every html element should look at perfect position with good style effect. CSS script will help you to do this.

CSS CODING:

 

294 total views, 1 views today

    Print       Email

About the author

Author

The Ultimate Website For All types of Latest Inventions Happening In this World.

  • http://www.bing.com/ Cheyanne

    A provocative inigsht! Just what we need!

You might also like...

jetpack-wordpress-570x427.png

Top WordPress Plugins For Lightning Speed Blog and SEO

Read More →