Custom widgets



 SonyDaman

How to make widgets in WordPress

1. first of all you need to find out the widget.php in your theme . if there is no widget.php you can create your own.
copy and paste the given code in widget.php

class bottom_widget_quote extends WP_Widget {
// Create Widget
function widget() {
parent::WP_Widget(false, $name = ‘Widget Name’, array(‘description’ => ”));
}
// Widget Content
function widget($args, $instance) {
extract( $args );
$title= strip_tags($instance[‘title’]);
$details= strip_tags($instance[‘details’]);
?>




}

// Update and save the widget
function update($new_instance, $old_instance) {
return $new_instance;
}
// If widget content needs a form
function form($instance) {
//widgetform in backend
$title= strip_tags($instance[‘title’]);
$details= strip_tags($instance[‘details’]);
?>


get_field_id(‘title’); ?>” name=”get_field_name(‘title’); ?>” type=”text” value=”” />



get_field_id(‘details’); ?>” name=”get_field_name(‘details’); ?>” type=”text” value=”” />


}

}
register_widget(‘bottom_widget_quote’);

this code is enough to register your widget .
2. second step is to declare a space in your wp-admin dashboard for that you need to copy and paste the following code in your function.php
if ( function_exists(‘register_sidebar’) )
register_sidebar( array(
‘name’ => __( ‘Widget Name’),
‘id’ => ‘mycustomwidgetarea5′,
‘description’ => __( ‘An optional widget area for your site footer’, ‘twentyeleven’ ),
‘before_widget’ => ‘




‘after_widget’ => “
”,
‘before_title’ => ‘

’,

‘after_title’ => ‘’,
) );
3. now the third step is to display the content of your widget . for that you need to copy and paste the following code to your desired place in you theme 

// Custom widget Area Start
if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(‘Widget Name’) ) : ?>

// Custom widget Area End
?>

0 comments: