WP Custom Widget
How to Create WP Custom Widget
In this tune how to create wp custom widget, the widget show wp dashboard appearance to widget section. This is important to learn this function because sometimes need to the widget to make user friendly wp back end.
Step-1: To create a custom widget in wordpress appearance widget area basically must need below function.
class clivern_custom_widget extends WP_Widget { function clivern_custom_widget(){ //process widget } function form($instance){ //show widget form in admin panel } function update($new_instance, $old_instance) { //update widget settings } function widget($args, $instance) { //display widget } }
Here, class clivern_custom_widget extends WP_Widget{} Creating the widget. function form($instance){} create back-end admin form. function update($new_instance, $old_instance){} Updating widget replacing old instances with new. function widget($args, $instance){} Creating widget front-end, that is where the action happens.
Step-2: A complete demo widget function given below. The full function taking in your theme function.php file.
class jebaWidgetTest extends WP_Widget { function jebaWidgetTest() { $widget_ops = array('classname' => 'jebaWidgetTest', 'description' => 'Here goes Jeba widget description' ); $this->WP_Widget('jebaWidgetTest', 'Jeba Widget Test', $widget_ops); } function form($instance) { $instance = wp_parse_args( (array) $instance, array( 'title' => 'H1 Title', 'text' => 'text text text' ) ); $title = $instance['title']; $text = $instance['text']; ?> <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p> <p><label for="<?php echo $this->get_field_id('text'); ?>">Title1: <textarea class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>" type="text" value="<?php echo attribute_escape($text); ?>"></textarea></label></p> <?php } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = $new_instance['title']; $instance['text'] = $new_instance['text']; return $instance; } function widget($args, $instance) { extract($args, EXTR_SKIP); echo $before_widget; $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']); $text = empty($instance['text']) ? ' ' : apply_filters('widget_text', $instance['text']); if (!empty($title)) echo $before_title . $title . $after_title;; if (!empty($text)) echo $text ;; // WIDGET CODE GOES HERE echo "[jeba_shortcode]"; echo $after_widget; } } add_action( 'widgets_init', create_function('', 'return register_widget("jebaWidgetTest");') );
Here, jebaWidgetTest this is a function name. In [jeba_shortcode] you can use your shortcode or if you need not delete it.
Adn finally add_action the widget in widgets_init.
Hopefully the custom widget function understand easily. If not understand comment here.
Comments are closed