How to Create WordPress Custom Widgets?
Last Updated :
06 Jun, 2024
Creating custom widgets in WordPress can significantly enhance your website's functionality by allowing you to add specific features and content to your sidebar or other widgetized areas. This article will guide you through the process of creating custom widgets, explaining different approaches and providing clear examples for each method.
There are three primary approaches to creating custom widgets in WordPress:
By using a plugin
- Go to your WordPress Dashboard.
- Navigate to "Plugins" > "Add New."
- Search for "Widget Options" or "SiteOrigin Widgets Bundle."
- Click "Install Now" and then "Activate."
- Follow the plugin’s instructions to create a custom widget.
- Use the plugin’s interface to define the content and settings of your widget.
- Go to "Appearance" > "Widgets."
- Drag and drop your custom widget to the desired widgetized area.

Syntax:
No coding is required for this approach as it is handled through the plugin’s interface.
Example:
Using "SiteOrigin Widgets Bundle":
- Install and Activate: Go to "Plugins" > "Add New" > Search "SiteOrigin Widgets Bundle" > Install > Activate.
- Create Widget: Navigate to "Plugins" > "SiteOrigin Widgets" > Create New Widget.
- Add to Sidebar: Go to "Appearance" > "Widgets" > Drag your new widget to the sidebar.
Output: The widget will appear in the widgetized areas defined by the plugin, fully functional based on the configuration settings you applied through the plugin interface.
By Writing Code in the Theme's functions.php File
For more control, you can create custom widgets by adding code to your theme’s functions.php file. This approach requires basic PHP and WordPress coding knowledge.
Step 1: Open functions.php:
- Navigate to your theme’s directory.
- Open functions.php in a code editor.
- Define a new widget class that extends WP_Widget.
- Register the widget using widgets_init action hook.
Example: Add to functions.php. Save the functions.php File and go to "Appearance" > "Widgets" in your WordPress dashboard. You should see "My Custom Widget" available to add to your sidebar.
PHP
// Define the widget class
class My_Custom_Widget extends WP_Widget {
// Widget settings and initialization
function __construct() {
parent::__construct(
'my_custom_widget',
esc_html__('My Custom Widget', 'text_domain'),
array('description' => esc_html__('A Custom Widget',
'text_domain'), )
);
}
// Front-end display of widget
public function widget($args, $instance) {
echo $args['before_widget'];
if (!empty($instance['title'])) {
echo $args['before_title'] . apply_filters('widget_title',
$instance['title']) . $args['after_title'];
}
echo esc_html__('Hello, World!', 'text_domain');
echo $args['after_widget'];
}
// Back-end widget form
public function form($instance) {
$title = !empty($instance['title']) ? $instance['title'] :
esc_html__('New title', 'text_domain');
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>">
<?php esc_attr_e('Title:', 'text_domain'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>"
name="<?php echo esc_attr($this->get_field_name('title')); ?>"
type="text" value="<?php echo esc_attr($title); ?>">
</p>
<?php
}
// Save widget form values
public function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = (!empty($new_instance['title'])) ?
strip_tags($new_instance['title']) : '';
return $instance;
}
}
// Register the widget
function register_my_custom_widget() {
register_widget('My_Custom_Widget');
}
add_action('widgets_init', 'register_my_custom_widget');
Output: After adding the code to functions.php, the widget will be available in the "Widgets" section of the WordPress admin, ready to be dragged into any widget area.
OutputBy using a Custom Plugin
Creating a custom plugin for your widget provides modularity and reusability. This approach is ideal for distributing your widget or maintaining it separately from your theme.
Step 1: Create a Plugin Folder:
- Navigate to wp-content/plugins directory.
- Create a new folder named my-custom-widget.
Step 2: Create the Main Plugin File:
- Inside my-custom-widget folder, create a file named my-custom-widget.php.
- Add plugin header information and widget code.
Example: Create and add code to my-custom-widget.php:
PHP
<?php
/*
Plugin Name: My Custom Widget
Description: A simple custom widget
Version: 1.0
Author: Your Name
*/
// Define the widget class
class My_Custom_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'my_custom_widget',
esc_html__('My Custom Widget', 'text_domain'),
array('description' => esc_html__('A Custom Widget',
'text_domain'), )
);
}
// Front-end display of widget
public function widget($args, $instance) {
echo $args['before_widget'];
if (!empty($instance['title'])) {
echo $args['before_title'] . apply_filters('widget_title',
$instance['title']) . $args['after_title'];
}
echo esc_html__('Hello, World!', 'text_domain');
echo $args['after_widget'];
}
// Back-end widget form
public function form($instance) {
$title = !empty($instance['title']) ? $instance['title'] :
esc_html__('New title', 'text_domain');
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>">
<?php esc_attr_e('Title:', 'text_domain'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>"
name="<?php echo esc_attr($this->get_field_name('title')); ?>"
type="text" value="<?php echo esc_attr($title); ?>">
</p>
<?php
}
// Save widget form values
public function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = (!empty($new_instance['title'])) ?
strip_tags($new_instance['title']) : '';
return $instance;
}
}
// Register the widget
function register_my_custom_widget() {
register_widget('My_Custom_Widget');
}
add_action('widgets_init', 'register_my_custom_widget');
?>
Activate the Plugin:
- Go to "Plugins" in your WordPress dashboard.
- Activate "My Custom Widget."
- Navigate to "Appearance" > "Widgets" to add your new widget to a sidebar.
Output: Activating the custom plugin will register the new widget, making it available in the "Widgets" section of the WordPress admin, where it can be placed in any widgetized area.
Conclusion
By following these approaches, you can create custom widgets in WordPress, providing enhanced functionality and unique content presentation on your website. Each method offers different levels of control and complexity, allowing you to choose the best approach for your needs.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read