23 May 2011
How to make a widget that has one textbox and custom html output in WordPress.
This has to go to top of your plugin file, wordpress uses this to identify the plugin. You can change te variables as you please.
Source code viewer
  1. <?php
  2. /**
  3.  * Bootstrap file for the plugin.
  4.  *
  5.  * @package My Widget
  6.  *
  7.  * @wordpress-plugin
  8.  * Plugin Name: My Plugin
  9.  * Plugin URI: http://browse-tutorials.com
  10.  * Description: My plugin description
  11.  * Version: 1.0.0
  12.  * Author: Browse-Tutorials
  13.  * Author URI: http://browse-tutorials.com
  14.  * License: GPL-2.0+
  15.  * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
  16.  */
  17. ?>
Programming Language: PHP
Simple widget with one text field in the form and it displays the content of that textbox.
Source code viewer
  1. <?php
  2. /**
  3.  * Create a class for the widget.
  4.  */
  5. class My_text_Widget extends WP_Widget {
  6.  
  7. /**
  8. * Sets up a new instance of the widget.
  9. */
  10. public function __construct() {
  11. $widget_ops = array( 'description' => __( 'My first widget.', 'my_locale' ) );
  12. parent::__construct(
  13. // Optional Base ID for the widget, lower case, if left empty a portion of the widget's class name will be used. Has to be unique.
  14. 'widget',
  15. // Name for the widget displayed on the configuration page.
  16. __( 'My first widget', 'my_locale' ),
  17. $widget_ops
  18. );
  19. }
  20.  
  21. /**
  22. * Outputs the content for the current widget instance.
  23. * @param array $args Display arguments including 'before_title', 'after_title',
  24. * 'before_widget', and 'after_widget'.
  25. * @param array $instance Settings for the current Search widget instance.
  26. */
  27. function widget( $args, $instance ) {
  28. // http://phpdoc.wordpress.org/trunk/WordPress/Widgets/WP_Widget.html#widget
  29. extract( $args, EXTR_SKIP );
  30. $text = empty( $instance['text'] ) ? ' ' : apply_filters( 'widget_title', $instance['text'] );
  31. if ( ! empty( $text ) ) {
  32. echo $text;
  33. }
  34. }
  35. /**
  36. * This function should check that $new_instance is set correctly. The newly
  37. * calculated value of $instance should be returned. If "false" is returned,
  38. * the instance won't be saved/updated.
  39. * @param array $new_instance
  40. * @param array $old_instance
  41. * @return array
  42. */
  43. function update( $new_instance, $old_instance ) {
  44. // http://phpdoc.wordpress.org/trunk/WordPress/Widgets/WP_Widget.html#update
  45. $instance = $old_instance;
  46. $instance[ 'text' ] = strip_tags( $new_instance['text'] );
  47. return $instance;
  48. }
  49. /**
  50. * Widget form on widgets page in admin panel.
  51. * @param array $instance
  52. * @return void
  53. */
  54. function form( $instance ) {
  55. $instance = wp_parse_args( (array) $instance, array( 'text' => '' ) );
  56. $text = strip_tags( $instance['text'] );
  57. echo '
  58. <p><label for="' . $this->get_field_id( 'text' ) . '">
  59. Text:
  60. <input class="widefat" id="' . $this->get_field_id( 'text' ) . '" name="' . $this->get_field_name( 'text' ) . '" type="text" value="' . attribute_escape( $text ) . '" />
  61. </label></p>';
  62. }
  63. }
  64. // Load your widget into WordPress.
  65. add_action( 'widgets_init', 'my_text_widget_init' );
  66.  
  67. function my_text_widget_init() {
  68. register_widget( 'My_text_Widget' );
  69. }
Programming Language: PHP