Basic of Building Custom Widgets in WordPress


Building WP widgets is same as building a plugin in WP but it is more simple and straightforward compare to Plugins . All you need to do is have a single file in which all the PHP goes and it’s easier to code than a plugin which can have more than one file. There are three major functions of a widget which can be broken down into widget, update and form.

Most important function for creating widget

  • function widget()
  • function update()
  • function form()

Basic Structure
The basic outline of our widget is very simple and there are a handful of functions that you’ll need to know. The bare bone structure of our widget is something like this:

  • add_action( ‘widgets_init’, ‘register_my_widget’ ); // function to load my widget
  • function register_my_widget() {} // function to register my widget
  • class My_Widget extends WP_Widget () {} // The example widget class
  • function My_Widget() {} // Widget Settings
  • function widget() {} // display the widget
  • function update() {} // update the widget
  • function form() {} // and of course the form for the widget options