| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | namespace Foo; |
|---|
| 4 | |
|---|
| 5 | use WP_Widget; |
|---|
| 6 | |
|---|
| 7 | /** |
|---|
| 8 | * Adds Foo_Widget widget. |
|---|
| 9 | */ |
|---|
| 10 | class Widget extends WP_Widget { |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * Register widget with WordPress. |
|---|
| 14 | */ |
|---|
| 15 | function __construct() { |
|---|
| 16 | parent::__construct( |
|---|
| 17 | __CLASS__, // Base ID |
|---|
| 18 | esc_html__( 'Widget Title', 'text_domain' ), // Name |
|---|
| 19 | array( 'description' => esc_html__( 'A Foo Widget', 'text_domain' ), ) // Args |
|---|
| 20 | ); |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * Front-end display of widget. |
|---|
| 25 | * |
|---|
| 26 | * @see WP_Widget::widget() |
|---|
| 27 | * |
|---|
| 28 | * @param array $args Widget arguments. |
|---|
| 29 | * @param array $instance Saved values from database. |
|---|
| 30 | */ |
|---|
| 31 | public function widget( $args, $instance ) { |
|---|
| 32 | echo $args['before_widget']; |
|---|
| 33 | if ( ! empty( $instance['title'] ) ) { |
|---|
| 34 | echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; |
|---|
| 35 | } |
|---|
| 36 | echo esc_html__( 'Hello, World!', 'text_domain' ); |
|---|
| 37 | echo $args['after_widget']; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Back-end widget form. |
|---|
| 42 | * |
|---|
| 43 | * @see WP_Widget::form() |
|---|
| 44 | * |
|---|
| 45 | * @param array $instance Previously saved values from database. |
|---|
| 46 | * |
|---|
| 47 | * @return bool|string |
|---|
| 48 | */ |
|---|
| 49 | public function form( $instance ) { |
|---|
| 50 | $title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'text_domain' ); |
|---|
| 51 | ?> |
|---|
| 52 | <p> |
|---|
| 53 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label> |
|---|
| 54 | <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 ); ?>"> |
|---|
| 55 | </p> |
|---|
| 56 | <?php |
|---|
| 57 | |
|---|
| 58 | return true; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * Sanitize widget form values as they are saved. |
|---|
| 63 | * |
|---|
| 64 | * @see WP_Widget::update() |
|---|
| 65 | * |
|---|
| 66 | * @param array $new_instance Values just sent to be saved. |
|---|
| 67 | * @param array $old_instance Previously saved values from database. |
|---|
| 68 | * |
|---|
| 69 | * @return array Updated safe values to be saved. |
|---|
| 70 | */ |
|---|
| 71 | public function update( $new_instance, $old_instance ) { |
|---|
| 72 | $instance = array(); |
|---|
| 73 | $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
|---|
| 74 | |
|---|
| 75 | return $instance; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | } // class Foo_Widget |
|---|
| 79 | |
|---|
| 80 | // register Foo_Widget widget |
|---|
| 81 | add_action( 'widgets_init', function () { |
|---|
| 82 | register_widget( new Widget ); |
|---|
| 83 | } ); |
|---|