Custom widget register in WordPress

Have you ever wanted to create a custom widget for your WordPress website? While WordPress does include a built-in text widget, which you can use to display HTML along with embedded CSS and JavaScript, if you want to do anything more than that the text widget just won’t cut it.

WordPress widgets are created with a bit of object-oriented programming. The WP_Widget class is extended to create each widget. The WP_Widget class includes close to 20 different methods. However, for basic widgets, you really only need to use four of them:

  • __construct(): registers basic widget information.
  • widget(): contains the widget output — what you actually see on your site front end when the widget is added to a widget area.
  • form(): defines the widget settings displayed in the WordPress admin area.
  • update(): updates widget settings when new settings are saved in the WordPress admin area.

In addition to these four methods, the add_action function is used to tie the custom widget function to the widgets_init hook.

Here is a live example of creating Widget. Follow this snippet and adjust it according to your needs.

Scroll to Top