Codementor Events

Easy Guide to Creating Your Own Custom WordPress Widget

Published Sep 08, 2017
Easy Guide to Creating Your Own Custom WordPress Widget

WordPress, a content management system (CMS), has inspired many users and become the preferred way to set up websites. There are many website builders available, but WordPress, Wix, and Squarespace are still the most preferred.

You can customize and design your own theme with WordPress' built-in theme builders, plugins, and widgets. For WordPress beginners, widgets afford a go-to option — you won't be required to learn the PHP language or comprehend code to put widgets to use.

Widgets are meant for beginners. However, many WordPress developers have preferred to develop customized Widgets to boost their websites' capabilities.

WordPress widgets have built-in fragments of code, which you can incorporate in the sidebars, or widget-ready zones in your websites. They are like modules, which can be used to include dissimilar essentials by dragging and dropping them into the interface. WordPress offers typical widgets that can be used with any WordPress theme.

Benefits of Using Custom Widgets

Need Based

Custom widgets are developed to meet a user's specific requirements and also to improve the functionalities of WordPress widgets that are already in use. Web developers and WordPress theme creators sometimes feel the default widgets are inadequate — hence the need to design their own widgets and integrate them with their website.

Customization

Many skilled developers have persistently striven for years to refine the structure and, eventually, fashion all capable WP 2.8. The improved WordPress platform offers a comprehensive widget named WP Widget. This value-added edition is comprised of five segments, with each one an essential phase in developing customized widgets.

Enhanced functionality

Your website will be endowed with greater and better qualities, which your clientele will appreciate and be more inclined to use continuously.

Enhanced user experience

Including new widgets in your sidebars could possibly render the browsing experience easier for users.

Upward trend in SEO

Depending on the efficacy and worth of your custom widget, more people will come back to your site for its remarkable functionality. Web crawlers will spot an upsurge in your total visitor number, which may get you greater search rankings.

How to Design and Customize a Widget

At the onset, you need to make sure the WordPress development environment is available for setting up a WordPress custom widget for a website.

The entire process of designing and developing WordPress widgets can be divided into five phases. Each phase is an essential stage for crafting custom widgets.

Primary Information

The initial phase specifies the widget ID, name, and description. It employs the function _construct() to place the primary widget info and preferences. It's in this phase that CSS or JavaScript is cited.

Appearance

The optical basics relating to the widget are determined and the HTML code is shown through widget(). To put it simply, this outlines how the user will get a feel for the widget's look.

Structure

This section, delineated in form(), deals with the procedural tasks of the widget, especially the Edit interface in the Admin Widgets console.

Execute

This part relates to the update() phase that executes and authenticates the records of the widget context and the environment from the back-end.

Record

Ultimately, this segment init() records the widget for use in WordPress. This phase is crucial to ensure that the custom widget is displayed in the group of existing widgets for sidebars.

You can add custom code in one of the following places:

a) A custom plugin, which you're required to put to use if you're going to need to use the widget with more than one theme or on more than one website.

b) The functions.php file of the active theme, which has to be either a child theme or a completely custom theme.

Steps To Create a Custom Widget:

1. Add an extended WP_Widget class

Example:

<?php
class custom_Widget extends WP_Widget {
}
?>

2. Add primary information to the _construct function

Example:

public function __construct() {

parent::__construct(
‘new_widget’,
__( ‘Custom Widget’, ‘new widget’ ),
array(
‘classname’ =>‘new_widget’,
‘description’ => __( ‘Enter description for your new widget’, ‘customdomain’ )
)
);

load_plugin_textdomain( ‘newwidget’, false, basename( dirname( __FILE__ ) ) . ‘/languages’ );

}

3. Define the widget's structure using form()

Example:

public function form( $instance ) {

$title = esc_attr( $instance[‘header’] );
$paragraph = esc_attr( $instance[‘description’] );
?>

<p>
<label for=”<?php echo $this->get_field_id(‘header’); ?>”><?php _e(‘Header:’); ?></label>
<br>
<input type=”text” value=”<?php echo $title; ?>” name=”<?php echo $this->get_field_name(‘header’); ?>” id=”<?php echo $this->get_field_id(‘header’); ?>” />
</p>
<p>
<label for=”<?php echo $this->get_field_id(‘description’); ?>”><?php _e(‘Text’); ?></label>
<br>
<textarea rows=”10″ cols=”16″ name=”<?php echo $this->get_field_name(‘description’); ?>” id=”<?php echo $this->get_field_id(‘description’); ?>”><?php echo $paragraph; ?></textarea>
</p>

<?php
}

4. Define the widget's appearance using function widget()

Example:

public function widget( $args, $instance ) {

extract( $args );

$title = apply_filters( ‘widget_title’, $in-stance[‘header’] );
$paragraph = $instance[‘description’];

echo $before_widget;
if ( $title ) {echo $before_title . $title . $af-ter_title;}
if ( $paragraph ) {echo $before_paragraph . $paragraph . $after_paragraph;}
echo $after_widget;

}

5. Execute the widget's records using the update() function

Example:

public function update( $new_instance, $old_instance ) {

$instance = $old_instance;

$instance[‘header’] = strip_tags( $new_instance[‘header’] );
$instance[‘description’] = strip_tags( $new_instance[‘description’] );

return $instance;

}

6. Add the custom widget in WordPress

Example:

add_action( ‘widgets_init’, function(){
register_widget( ‘custom_Widget’ );
});

7. Link your widget to functions.php

Create a PHP file (customwidget.php) and add the custom widget codes. Upload the customwidget.php file in the theme folder, and add: in functions.php:
require(“widgets/customwidget.php”).

You'll be able to see the widget in the Widgets section of the Admin console. You can drag the custom widget tab to your desired sidebar.

As you've just seen, developing the WordPress custom widget is easy. Are you ready to create your own widget?

These seven steps are the basic steps you can follow to create any type of widget. Keep one thing in mind, though — you're developing these custom widgets to give your visitors a beautiful experience on your site, not for your own comfort.

Discover and read more posts from Kiera Hayes
get started
post commentsBe the first to share your opinion
Show more replies