How to make new widget in Wordrpress? Explain with examples.
class My_Widget extends WP_Widget {
public function __construct() {
// widget actual processes
parent::WP_Widget(false,'Tags by Category','Simple widget to extract tags by Category');
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
$title = $instance['title'];
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('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 esc_attr($title); ?>" /></label></p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
}
public function widget( $args, $instance ) {
// outputs the content of the widget
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
echo $before_widget;
if ( $title )
echo "<h3 class='widget-title'>".$before_title . $title . $after_title."</h3>";
query_posts('category_name=written-articles');
if (have_posts()) : while (have_posts()) : the_post();
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$all_tags_arr[] = $tag -> name; //USING JUST $tag MAKING $all_tags_arr A MULTI-DIMENSIONAL ARRAY, WHICH DOES WORK WITH array_unique
$all_link[] = get_tag_link($tag->term_id);
}
}
endwhile; endif;
$tags_arr = array_unique($all_tags_arr); //REMOVES DUPLICATES
$tags_link = array_unique($all_link);
//echo '<pre>'.print_r($tags_arr, true).'</pre>'; //OUTPUT FINAL TAGS FROM CATEGORY
//echo '<pre>'.print_r($tags_link, true).'</pre>';
echo '<div class="tagcloud">';
for($i = 0; $i<count($tags_arr);$i++)
{
echo '<a href="'.$tags_link[$i].'">'.$tags_arr[$i].'</a>'." ";
}
echo "</div>\n";
}
}
register_widget( 'My_Widget' );
public function __construct() {
// widget actual processes
parent::WP_Widget(false,'Tags by Category','Simple widget to extract tags by Category');
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
$title = $instance['title'];
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('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 esc_attr($title); ?>" /></label></p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
}
public function widget( $args, $instance ) {
// outputs the content of the widget
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
echo $before_widget;
if ( $title )
echo "<h3 class='widget-title'>".$before_title . $title . $after_title."</h3>";
query_posts('category_name=written-articles');
if (have_posts()) : while (have_posts()) : the_post();
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$all_tags_arr[] = $tag -> name; //USING JUST $tag MAKING $all_tags_arr A MULTI-DIMENSIONAL ARRAY, WHICH DOES WORK WITH array_unique
$all_link[] = get_tag_link($tag->term_id);
}
}
endwhile; endif;
$tags_arr = array_unique($all_tags_arr); //REMOVES DUPLICATES
$tags_link = array_unique($all_link);
//echo '<pre>'.print_r($tags_arr, true).'</pre>'; //OUTPUT FINAL TAGS FROM CATEGORY
//echo '<pre>'.print_r($tags_link, true).'</pre>';
echo '<div class="tagcloud">';
for($i = 0; $i<count($tags_arr);$i++)
{
echo '<a href="'.$tags_link[$i].'">'.$tags_arr[$i].'</a>'." ";
}
echo "</div>\n";
}
}
register_widget( 'My_Widget' );
0 comments:
Feel free to contact the admin for any suggestions and help.