[26-Feb-2026 00:31:13 America/Chicago] PHP Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH' (this will throw an Error in a future version of PHP) in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [26-Feb-2026 00:31:13 America/Chicago] PHP Warning: require_once(ABSPATHwp-admin/includes/upgrade.php): failed to open stream: No such file or directory in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [26-Feb-2026 00:31:13 America/Chicago] PHP Fatal error: require_once(): Failed opening required 'ABSPATHwp-admin/includes/upgrade.php' (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [01-Mar-2026 07:45:51 America/Chicago] PHP Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH' (this will throw an Error in a future version of PHP) in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [01-Mar-2026 07:45:51 America/Chicago] PHP Warning: require_once(ABSPATHwp-admin/includes/upgrade.php): failed to open stream: No such file or directory in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [01-Mar-2026 07:45:51 America/Chicago] PHP Fatal error: require_once(): Failed opening required 'ABSPATHwp-admin/includes/upgrade.php' (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [01-Mar-2026 18:48:26 America/Chicago] PHP Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH' (this will throw an Error in a future version of PHP) in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [01-Mar-2026 18:48:26 America/Chicago] PHP Warning: require_once(ABSPATHwp-admin/includes/upgrade.php): failed to open stream: No such file or directory in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [01-Mar-2026 18:48:26 America/Chicago] PHP Fatal error: require_once(): Failed opening required 'ABSPATHwp-admin/includes/upgrade.php' (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [06-Mar-2026 13:14:53 America/Chicago] PHP Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH' (this will throw an Error in a future version of PHP) in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [06-Mar-2026 13:14:53 America/Chicago] PHP Warning: require_once(ABSPATHwp-admin/includes/upgrade.php): failed to open stream: No such file or directory in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [06-Mar-2026 13:14:53 America/Chicago] PHP Fatal error: require_once(): Failed opening required 'ABSPATHwp-admin/includes/upgrade.php' (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [08-Mar-2026 11:47:41 America/Chicago] PHP Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH' (this will throw an Error in a future version of PHP) in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [08-Mar-2026 11:47:41 America/Chicago] PHP Warning: require_once(ABSPATHwp-admin/includes/upgrade.php): failed to open stream: No such file or directory in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [08-Mar-2026 11:47:41 America/Chicago] PHP Fatal error: require_once(): Failed opening required 'ABSPATHwp-admin/includes/upgrade.php' (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [20-Mar-2026 17:37:48 America/Chicago] PHP Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH' (this will throw an Error in a future version of PHP) in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [20-Mar-2026 17:37:48 America/Chicago] PHP Warning: require_once(ABSPATHwp-admin/includes/upgrade.php): failed to open stream: No such file or directory in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [20-Mar-2026 17:37:48 America/Chicago] PHP Fatal error: require_once(): Failed opening required 'ABSPATHwp-admin/includes/upgrade.php' (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [20-Mar-2026 17:37:53 America/Chicago] PHP Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH' (this will throw an Error in a future version of PHP) in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [20-Mar-2026 17:37:53 America/Chicago] PHP Warning: require_once(ABSPATHwp-admin/includes/upgrade.php): failed to open stream: No such file or directory in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [20-Mar-2026 17:37:53 America/Chicago] PHP Fatal error: require_once(): Failed opening required 'ABSPATHwp-admin/includes/upgrade.php' (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [27-Mar-2026 14:07:52 America/Chicago] PHP Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH' (this will throw an Error in a future version of PHP) in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [27-Mar-2026 14:07:52 America/Chicago] PHP Warning: require_once(ABSPATHwp-admin/includes/upgrade.php): failed to open stream: No such file or directory in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [27-Mar-2026 14:07:52 America/Chicago] PHP Fatal error: require_once(): Failed opening required 'ABSPATHwp-admin/includes/upgrade.php' (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [29-Mar-2026 10:21:48 America/Chicago] PHP Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH' (this will throw an Error in a future version of PHP) in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [29-Mar-2026 10:21:48 America/Chicago] PHP Warning: require_once(ABSPATHwp-admin/includes/upgrade.php): failed to open stream: No such file or directory in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 [29-Mar-2026 10:21:48 America/Chicago] PHP Fatal error: require_once(): Failed opening required 'ABSPATHwp-admin/includes/upgrade.php' (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/touchmob/crazyleafdesign.com/blog/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/app-notification/classes/DbMigration.php on line 2 {"id":3738,"date":"2011-02-04T20:21:34","date_gmt":"2011-02-04T18:21:34","guid":{"rendered":"https:\/\/crazyleafdesign.com\/blog\/?p=3738"},"modified":"2013-02-20T00:23:54","modified_gmt":"2013-02-19T22:23:54","slug":"creating-a-widget-in-wordpress-the-basic-stuff-with-an-example","status":"publish","type":"post","link":"https:\/\/www.crazyleafdesign.com\/blog\/creating-a-widget-in-wordpress-the-basic-stuff-with-an-example\/","title":{"rendered":"Creating a Widget in WordPress – The Basic Stuff with an Example"},"content":{"rendered":"
Widgets in WordPress <\/strong>are small components that one can write which from the admin area of wordpress lets the admin to drag and drop a widget on any of the registered widgetize area of the theme. Then the widget will perform its function echo its output on the area where it is dragged and dropped.<\/p>\n All the registered widgets are seen in the widgets menu in the admin section as shown below.<\/p>\n Creating widgets in WordPress <\/strong>is very simple. We are now going to create a widget in WordPress <\/strong>which will display a few newest posts posted in WordPress<\/strong>.<\/p>\n Here is the code of the widget which you can put in the functions.php<\/strong> file of your theme :<\/p>\n To create a widget you have to create a class which extends the base class WP_Widget.<\/p>\n <\/p>\n In the constructor we give id and the name which are unique to this widget . With these values we can also pass some other option for our widget like in our example we are passing the description of the widget which will be displayed.<\/p>\n To display the options in the admin you have to override the function function form($instance) in you widget class . In this function we have created two fields as options to the widget. One is the title and second is how many newest posts should be displayed by your widget. Once you have written this function and drag and drop your widget on one of the widgetize area you should be able to see your form as shown below.<\/p>\n To update the options of your widget once save is clicked on the widget shown above we have to override the function function update($new_instance, $old_instance)<\/strong>.<\/p>\n In this we have to add the values $intance variable and the options will be saved automatically.<\/p>\n We have to add the actual functionality of the widget in the function function widget($args, $instance)<\/strong> . Here we are first removing the options for the widget which were saved. We remove the title option and display it . Then we get the count and then using the WordPress <\/strong>api query_posts we get the newest posts and then echo them.<\/p>\n Once this is done the widget is ready. Then we have to register the widget with the WordPress <\/strong>system using the api register_widget(‘trywidget’)<\/strong>; .<\/p>\n Now the widget is ready and after it is dragged and dropped on a widgetize area in the admin the output of the widget can be seen in the front end.<\/p>\n Widgets are very powerful components of the wordpress systems. Now creating widgets with the wordpress api has become very simple. This has made developing widgets for wordpress fast and very easy.<\/p>\n","protected":false},"excerpt":{"rendered":" Widgets in WordPress are small components that one can write which from the admin area of wordpress lets the admin to drag and drop a widget on any of the registered widgetize area of the theme. Then the widget will perform its function echo its output on the area where it is dragged and dropped. […] More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[40],"tags":[],"adace-sponsor":[],"class_list":{"0":"post-3738","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-programming"},"acf":[],"yoast_head":"\n
<\/p>\nCreating widgets in wordpress<\/h2>\n
\r\nclass trywidget extends WP_Widget {\r\nfunction trywidget() {\r\n\/\/ widget actual processes\r\n$widget_ops = array('classname' => 'trywidget', 'description' => 'description for trywidget' );\r\n$this->WP_Widget('trywidget', 'trywidget', $widget_ops);\r\n}\r\n\r\nfunction form($instance) {\r\n\/\/ outputs the options form on admin\r\n$defaults = array( 'title' => __('Latest posts'), 'count' => 0 );\r\n$instance = wp_parse_args( (array) $instance, $defaults );\r\n$count = isset($instance['count']) ? $instance['count'] :0;\r\n?>\r\n\r\n<label for="<?php echo $this->get_field_id( 'title' ); ?>"><\/label>\r\n<input id="<?php echo $this->get_field_id( 'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" \/>\r\n\r\n<label for="<?php echo $this->get_field_id( 'count' ); ?>"><\/label>\r\n<input id="<?php echo $this->get_field_id( 'count' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'count' ); ?>" value="<?php echo $instance['count']; ?>" \/>\r\n\r\n}\r\n\r\nfunction update($new_instance, $old_instance) {\r\n\/\/ processes widget options to be saved\r\n\r\n$instance = $old_instance;\r\n$instance['title'] = strip_tags( $new_instance['title'] );\r\n$instance['count'] = !empty($new_instance['count']) ? $new_instance['count'] : 0;\r\nreturn $instance;\r\n}\r\n\r\nfunction widget($args, $instance) {\r\n\/\/ outputs the content of the widget\r\nextract( $args );\r\n$title = apply_filters('widget_title', $instance['title'] );\r\n$count = $instance['count'] ? $instance['count'] : '0';\r\necho $before_widget;\r\nif ( $title )\r\necho $before_title . $title . $after_title;\r\necho '\r\n<ul>';\r\n\r\nquery_posts('showposts='.$count);\r\nwhile ( have_posts() ) : the_post(); ?>\r\n<li><a title="Permanent Link to <?php the_title_attribute(); ?>" rel="bookmark" href="<?php the_permalink() ?>"><\/a><\/li>\r\necho '<\/ul>\r\n';\r\necho $after_widget;\r\n}\r\n\r\n}\r\nregister_widget('trywidget');\r\n<\/pre>\nConstructor<\/h2>\n
\r\nfunction trywidget() {\r\n\/\/ widget actual processes\r\n$widget_ops = array('classname' => 'trywidget', 'description' => 'description for trywidget' );\r\n$this->WP_Widget('trywidget', 'trywidget', $widget_ops);\r\n}\r\n\r\n<\/pre>\nGetting the widget options displayed in the admin section<\/h2>\n
\r\nfunction form($instance) {\r\n\/\/ outputs the options form on admin\r\n$defaults = array( 'title' => __('Latest posts'), 'count' => 0 );\r\n$instance = wp_parse_args( (array) $instance, $defaults );\r\n$count = isset($instance['count']) ? $instance['count'] :0;\r\n?>\r\n<p>\r\n<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?><\/label>\r\n<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" \/>\r\n<\/p>\r\n<p>\r\n<label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Number of post:' ); ?><\/label>\r\n<input id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" value="<?php echo $instance['count']; ?>" class="widefat" \/>\r\n<\/p>\r\n\r\n<?php\r\n}\r\n\r\n<\/pre>\n
<\/p>\nUpdating the options of your widget<\/h2>\n
\r\nfunction update($new_instance, $old_instance) {\r\n\/\/ processes widget options to be saved\r\n\r\n$instance = $old_instance;\r\n$instance['title'] = strip_tags( $new_instance['title'] );\r\n$instance['count'] = !empty($new_instance['count']) ? $new_instance['count'] : 0;\r\nreturn $instance;\r\n}\r\n<\/pre>\nActual functionality of the widget<\/h2>\n
\r\nfunction widget($args, $instance) {\r\n\/\/ outputs the content of the widget\r\nextract( $args );\r\n$title = apply_filters('widget_title', $instance['title'] );\r\n$count = $instance['count'] ? $instance['count'] : '0';\r\necho $before_widget;\r\nif ( $title )\r\necho $before_title . $title . $after_title;\r\necho '\r\n<ul>';\r\n\r\nquery_posts('showposts='.$count);\r\nwhile ( have_posts() ) : the_post(); ?>\r\n<li><a title="Permanent Link to <?php the_title_attribute(); ?>" rel="bookmark" href="<?php the_permalink() ?>"><\/a><\/li>\r\necho '<\/ul>\r\n';\r\necho $after_widget;\r\n}\r\n\r\n<\/pre>\n
<\/p>\nConclusion<\/h2>\n