wordpress add widget developer

Solutions on MaxInterview for wordpress add widget developer by the best coders in the world

showing results for - "wordpress add widget developer"
Simon
14 Jan 2018
1<?php
2 
3class My_Widget extends WP_Widget {
4 
5    function __construct() {
6 
7        parent::__construct(
8            'my-text'// Base ID
9            'My Text'   // Name
10        );
11 
12        add_action( 'widgets_init', function() {
13            register_widget( 'My_Widget' );
14        });
15 
16    }
17 
18    public $args = array(
19        'before_title'  => '<h4 class="widgettitle">',
20        'after_title'   => '</h4>',
21        'before_widget' => '<div class="widget-wrap">',
22        'after_widget'  => '</div></div>'
23    );
24 
25    public function widget( $args, $instance ) {
26 
27        echo $args['before_widget'];
28 
29        if ( ! empty( $instance['title'] ) ) {
30            echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
31        }
32 
33        echo '<div class="textwidget">';
34 
35        echo esc_html__( $instance['text'], 'text_domain' );
36 
37        echo '</div>';
38 
39        echo $args['after_widget'];
40 
41    }
42 
43    public function form( $instance ) {
44 
45        $title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( '', 'text_domain' );
46        $text = ! empty( $instance['text'] ) ? $instance['text'] : esc_html__( '', 'text_domain' );
47        ?>
48        <p>
49        <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php echo esc_html__( 'Title:', 'text_domain' ); ?></label>
50            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
51        </p>
52        <p>
53            <label for="<?php echo esc_attr( $this->get_field_id( 'Text' ) ); ?>"><?php echo esc_html__( 'Text:', 'text_domain' ); ?></label>
54            <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'text' ) ); ?>" type="text" cols="30" rows="10"><?php echo esc_attr( $text ); ?></textarea>
55        </p>
56        <?php
57 
58    }
59 
60    public function update( $new_instance, $old_instance ) {
61 
62        $instance = array();
63 
64        $instance['title'] = ( !empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
65        $instance['text'] = ( !empty( $new_instance['text'] ) ) ? $new_instance['text'] : '';
66 
67        return $instance;
68    }
69 
70}
71$my_widget = new My_Widget();
72?>
73