Lesson 38bis: Creating New Custom Widgets on WordPress

Usually, WordPress themes already have predefined Widget Areas. However, for some reason, you may need to create additional Widget Areas for blocks or shortcodes. There are two ways to create additional widget areas: by using the WP Custom Widget Area plugin and by writing code into the theme’s functions.php file.

1. Add an additional widget area using WP Custom Widget area Plugin

To install the WP Custom Widget Area plugin, follow these steps:

Step 1: Download the plugin here or here, or from the WP Custom Widget Area homepage.

Step 2: Install the plugin following the steps in the image below.

On the left menu, click on Plugin > Add New Plugin; then click Upload Plugin, and select Choose File. In the new window that opens, find and select the plugin you just downloaded, then click Install Now.

Step 3: Click “Activate” on the next screen to run the plugin.

Step 4: Create the widget area you want by following the steps as in the image below.

On the left menu, click on the newly added block of CWA Settings, then click Custom Widget Area. In this interface, enter the full name of the area to be created in Name box; the Id will be generated automatically so you can leave it as default; you can enter a description or leave it blank. Finally, click Create, and the newly created widget area will be displayed in the list.

Step 5: Check and use the area you created as shown in the image below.

2. Add an additional widget area with code into the function.php of the theme

First things first, you need to remember the following three functions if you want to create custom widgets:

function widget () This function is responsible for rendering front-end design or how your widget (HTML layout) will look like the front end of the website.

function form () This function allows us to create a form (usually contains HTML input form fields, i.e., textbox, select, etc.).

function update () This function gets called as soon as users hit the submit button if a form is available in the widget. It reloads the object through AJAX request and saves (or processes) your data according to your need.

Now, you need to create a file by the name of custom-widget.php with this code:

  1. <?php
  2. // Creating the widget
  3. class cw_widget extends WP_Widget
  4. {
  5. function __construct()
  6. {
  7. parent::__construct(
  8. // Base ID of your widget
  9. ‘cw_widget’,
  10. // Widget name will appear in UI
  11. __(‘Cloudways Widget’, ‘cw_widget_domain’),
  12. // Widget description
  13. array(
  14. ‘description’ => __(‘Sample widget based on Testing Widgets’,
  15. ‘cw_widget_domain’)
  16. ));
  17. }
  18. // Creating widget front-end
  19. // This is where the action happens
  20. public function widget($args, $instance)
  21. {
  22. $title = apply_filters(‘widget_title’, $instance[‘title’]);
  23. // before and after widget arguments are defined by themes
  24. echo $args[‘before_widget’];
  25. if (!empty($title))
  26. echo $args[‘before_title’] . $title . $args[‘after_title’];
  27. // This is where you run the code and display the output
  28. echo __($title, ‘cw_widget_domain’);
  29. echo $args[‘after_widget’];
  30. }
  31. // Widget Backend
  32. public function form($instance)
  33. {
  34. if (isset($instance[‘title’])) {
  35. $title = $instance[‘title’];
  36. } else {
  37. $title = __(‘New title’, ‘cw_widget_domain’);
  38. }
  39. // Widget admin form
  40. ?>
  41. <label for=”<?php
  42. echo $this->get_field_id(‘title’);
  43. ?>”><?php
  44. _e(‘Title:’);
  45. ?>
  46. <input class=“widefat” id=”<?php
  47. echo $this->get_field_id(‘title’);
  48. ?>” name=”<?php
  49. echo $this->get_field_name(‘title’);
  50. ?>” type=”text” value=”<?php
  51. echo esc_attr($title);
  52. ?>” />
  53. <?php
  54. }
  55. // Updating widget replacing old instances with new
  56. public function update($new_instance, $old_instance)
  57. {
  58. $instance = array();
  59. $instance[‘title’] = (!empty($new_instance[‘title’]))
  60. ? strip_tags($new_instance[‘title’]) : ;
  61. return $instance;
  62. }
  63. } // Class cw_widget ends here
  64. // Register and load the widget
  65. function cw_load_widget()
  66. {
  67. register_widget(‘cw_widget’);
  68. }
  69. add_action(‘widgets_init’, ‘cw_load_widget’);

You can integrate the custom widget in the function.php of your theme using the following code:

require_once('custom-widget.php');

In the end, you register and load the widget. Go to Appearance > Widgets. Here you can now see your own created custom widget. Drag and drop your widget in any sidebar of your theme.

Leave a Reply

Your email address will not be published. Required fields are marked *