Wordpress Text Widget with Class


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * Text Widget With CSS Class
  4.  *
  5.  */
  6.  
  7. class TextWidgetClass extends WP_Widget {
  8. //Constructor
  9. function TextWidgetClass() {
  10.  
  11. parent::WP_Widget(false, $name = 'Text Widget With Class');
  12.  
  13. }
  14.  
  15. // The Widget (front-end)
  16. function widget($args, $instance) {
  17.  
  18. extract( $args );
  19.  
  20. $title = apply_filters( 'widget_title', $instance['title'] );
  21. $body = apply_filters( 'widget_body', $instance['body'] );
  22. $the_class = apply_filters( 'widget_body', $instance['the_class'] );
  23. ?>
  24.  
  25. <li <?php echo 'id=""' . "class='text_widget_class $the_class'"; ?> >
  26. <?php if ( $title ) ?>
  27. <?php echo $before_title . $title . $after_title; ?>
  28. <?php echo '<p>' . $body . '</p>'; ?>
  29. </li>
  30. <?php
  31. }
  32.  
  33. // Udpate Widget
  34. function update($new_instance, $old_instance) {
  35. $instance = $old_instance;
  36. $instance['title'] = strip_tags($new_instance['title']);
  37. $instance['body'] = trim($new_instance['body']);
  38. $instance['the_class'] = strip_tags($new_instance['the_class']);
  39. return $instance;
  40. }
  41.  
  42. // Display Widget From
  43. function form($instance) {
  44. $title = esc_attr($instance['title']);
  45. $body = esc_attr($instance['body']);
  46. $the_class= esc_attr($instance['the_class']);
  47.  
  48. ?>
  49. <p>
  50. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  51. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
  52. </p>
  53. <p>
  54. <label for="<?php echo $this->get_field_id('the_class'); ?>"><?php _e('Class:'); ?></label>
  55. <input class="widefat" id="<?php echo $this->get_field_id('the_class'); ?>" name="<?php echo $this->get_field_name('the_class'); ?>" type="text" value="<?php echo $the_class; ?>" />
  56. </p>
  57. <p>
  58. <label for="<?php echo $this->get_field_id('body'); ?>"><?php _e('Body:'); ?></label>
  59. <textarea class="widefat" rows="16" colls="20" id="<?php echo $this->get_field_id('body'); ?>" name="<?php echo $this->get_field_name('body'); ?>"><?php echo $body; ?></textarea>
  60. </p>
  61.  
  62. <?php
  63. }
  64.  
  65. } // end class
  66.  
  67. // register the widget
  68. add_action('widgets_init', create_function('', 'return register_widget("TextWidgetClass");'));
  69.  
  70. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.