Home    Articles

 

How to set up widget in wordpress

 

If you want to create your own widget in wordpress, i will show you a very simple way to do it:

First of all you need to know that the widgets works with plugins. In order to build your widget you need to build simple plugin:
Create a folder called “my_widget” in wp-content/plugins.
In the new created folder, create a file called “my_widget.php“.

The file need to have the same name as the folder.

1) Create the plugin
Now add the following code:

<?php
/* 
Plugin Name: My widget
Plugin URI: http://www.w3bdeveloper.com
Description: Widget test
Author: Daniel Halmagean 
Version: 1.0 
Author URI: http://www.w3bdeveloper.com
*/

Save it. Go to your wordpress admin area and go to plugins.
Search for “My Widget” and click “Activate“.

Right now nothing is happening becouse you have not created your widget but you have created a way for your widget to be reconized by the wordpress.

2) Create the widget code

Add the following code under the pevious code:

# "wp_wid" is the widget name, it is used once.
class wp_wid extends WP_Widget 
{
    # constructor
    function wp_wid()
    {
         parent::WP_Widget(false, $name = __('My Widget', 'wp_widget_plugin'));
    }
    # widget form creation
    function form($instance) 
    { 
    	/* ... */
    }
    # widget update
    function update($new_instance, $old_instance) 
    {
        /* ... */
    }
    # widget display
    function widget($args, $instance) 
    {
    	echo '<p><b>This is my widget content</b></p>';
    }
}
// register widget
add_action('widgets_init', create_function('', 'return register_widget("wp_wid");'));
?>

Click save and go to apearance and search for a widget called “My Widget”.

set_wordpress_widget

Drag it into the left or right side of wordpress website.
The string “This is my widget content” should apear on your frontend.

That’s all.
Enjoy.

…first time posted on w3bdeveloper.com

by