Adding a widget template in WordPress

By | July 31, 2021

Widget templates in WordPress are particularly coded for showing the widget data on any page by assigning the widget template to that page easily, you can also show more data to that page by using any page builder, or by adding other stuff to that particular widget. You can get all the data and information on the page which has widget-template.

Let suppose, I have a contact form in a widget, and I want to show that contact form on all of my website pages then simply what will I do? I will assign the widget template to all of the pages and before that, I will code the widget template with a unique name.

How to make a widget template through code?

You need to create a file in your WordPress theme directory with the name  “template-widget.php ”, you may create it in either child theme or parent theme, but it’s recommended to use the child theme for these types of practices.

 

Add a few introduction lines for the template, note that these lines are mandatory as you can recognize your custom template easily.

<?php

/* This template is used to display template parts

* Template Name: widget Template

*/

get_header();

dynamic_sidebar(‘sidebar-ift’);  // you can change the name of sidebar.

get_footer();

?>

sidebar-.jpg

 

This code will add the sidebar only but would not show any data, we need to add some more code to the functions.php in the theme directory to make it workable.

 

 

 

function ift_widgets_init()

{

    register_sidebar(array(

        ‘name’          => __(‘Sidebar ift’, ‘theme_name’),

        ‘id’            => ‘sidebar-ift’,

        ‘description’   => ‘Main website widget’,

        ‘before_widget’ => ‘<aside id=”%1$s” class=”widget %2$s”>’,

        ‘after_widget’  => ‘</aside>’,

        ‘before_title’  => ‘<h3 class=”widget-title”>’,

        ‘after_title’   => ‘</h3>’,

    ));

}

add_action(‘widgets_init’, ‘ift_widgets_init’);

 

 

Leave a Reply

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