Guest User

Untitled

a guest
Jun 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. <?php
  2. /**
  3. * Adds Foo_Widget widget.
  4. */
  5. class Foo_Widget extends WP_Widget {
  6.  
  7. /**
  8. * Register widget with WordPress.
  9. */
  10. function __construct() {
  11. parent::__construct(
  12. 'foo_widget', // Base ID
  13. esc_html__( 'Widget Title', 'text_domain' ), // Name
  14. array( 'description' => esc_html__( 'A Foo Widget', 'text_domain' ), ) // Args
  15. );
  16. }
  17.  
  18. /**
  19. * Front-end display of widget.
  20. *
  21. * @see WP_Widget::widget()
  22. *
  23. * @param array $args Widget arguments.
  24. * @param array $instance Saved values from database.
  25. */
  26. public function widget( $args, $instance ) {
  27. echo $args['before_widget'];
  28. if ( ! empty( $instance['title'] ) ) {
  29. echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
  30. }
  31. echo esc_html__( 'Hello, World!', 'text_domain' );
  32. echo $args['after_widget'];
  33. }
  34.  
  35. /**
  36. * Back-end widget form.
  37. *
  38. * @see WP_Widget::form()
  39. *
  40. * @param array $instance Previously saved values from database.
  41. */
  42. public function form( $instance ) {
  43. $title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'text_domain' );
  44. ?>
  45. <p>
  46. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label>
  47. <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 ); ?>">
  48. </p>
  49. <?php
  50. }
  51.  
  52. /**
  53. * Sanitize widget form values as they are saved.
  54. *
  55. * @see WP_Widget::update()
  56. *
  57. * @param array $new_instance Values just sent to be saved.
  58. * @param array $old_instance Previously saved values from database.
  59. *
  60. * @return array Updated safe values to be saved.
  61. */
  62. public function update( $new_instance, $old_instance ) {
  63. $instance = array();
  64. update_option( 'test_hello_123', 'test' );
  65. $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
  66.  
  67. return $instance;
  68. }
  69.  
  70. } // class Foo_Widget
  71.  
  72. // register Foo_Widget widget
  73. function register_foo_widget() {
  74. register_widget( 'Foo_Widget' );
  75. }
  76.  
  77. add_action( 'widgets_init', 'register_foo_widget' );
Add Comment
Please, Sign In to add comment