Skip to navigation
Who Widgets works in wordpress
09.06.25
Widgets In this article A widget adds content and features to a widget area (also called a sidebar). Widget areas provide a way for users to customize their site. A widget area can appear on multiple pages or on only one page. Your theme might have just one widget area or many of them. Some examples of ways you can use widget areas are: Lay out a homepage using widgets. This allows site owners to decide what should appear in each section of their homepage. Create a footer that users can customize with their own content. Add a customizable sidebar to a blog. A widget is a PHP object that outputs some HTML. The same kind of widget can be used multiple times on the same page (e.g. the Text Widget). Widgets can save data in the database (in the options table). When you create a new kind of widget, it will appear in the user’s Administration Screens at Appearance > Widgets. The user can add the widget to a widget area and customize the widget settings from the WordPress admin. Built-in versus stand-alone widgets A set of widgets is included with the default WordPress installation. In addition to these standard widgets, extra widgets can be included by themes or plugins. An advantage of widgets built into themes or plugins is to provide extra features and increase the number of widgets. A disadvantage is that if a theme is changed or a plugin is deactivated, the plugin or theme widget’s functionality will be lost. However, the data and preferences from the widget will be saved to the options table and restored if the theme or plugin is reactivated. If you include a widget with your theme, it can only be used if that theme is active. If the user decides to change their theme they will lose access to that widget. However, if the widget is included with a plugin, the user can change their theme without losing access to the widget functionality. Anatomy of a Widget Visually, a widget comprises two areas: Title Area Widget Options For example, here is the layout of the built-in text widget in the admin and on the front-end: Sample Editable WidgetA widget form in the admin area. A widget as it appars to a site visitor.A widget as it appears to a site visitor. To create and display a widget, you need to do the following: Create your widget’s class by extending the standard WP_Widget class and some of its functions. Register your widget so that it’s made available in the Widgets screen. Make sure that your theme has at least one widget area in which to add the widgets. Your Widget Class The WP_Widget class is located in wp-includes/class-wp-widget.php Expand code Copy The documentation for each of these functions can be found in the widget class code: construct: Set up your widget with a description, name, and display width in your admin. widget: Process the widget options and display the HTML on your page. The $args parameter provides the HTML you can use to display the widget title class and widget content class. form: Display the form that will be used to set the options for your widget. If your widget doesn’t have any options, you can skip this function (although it is still best practice to include it even if it’s empty). update: Save the widget options to the database. If your widget doesn’t have any options, you can skip this function (although it is still best practice to include it even if it’s empty). Registering a Widget The register_widget() function is used to register a widget. Call this function using the widgets_init hook: Copy The HTML that wraps the widget, as well as the class for the title and widget content, is specified at the time you register the widget area using register_sidebar(). Examples Example Text Widget To build the text widget from the example at the beginning of this article. You will start by setting up a widget class that extends the WP_Widget class. In the class constructor you will call the parent constructor and pass it your widget’s base ID and name. Also in the class constructor you will hook into the widgets_init action to register your widget. Next you will declare the arguments you will use when creating your widget. There are four arguments you must define, before_title, after_title, before_widget, and after_widget. These arguments will define the code that wraps your widgets title and the widget itself. After defining your arguments, you will define the widgets function. This function takes two parameters, the $args array from before, and the $instance of the widget, and is the function that will process options from the form and display the HTML for the widget on the front-end of your site. In the example above the widget function simply outputs the widget title, while passing it through the widget_title filter. It then out puts a simple widget wrapper and the content of the widget’s text field. As outlined in the example, you can access the options from the widget that are stored in the $instance. Next you will define the form function. This function takes one parameter, $instance, and outputs the form that the user uses to create the widget in the Widgets admin screen. In the example above, the function starts by defining the $title and $text variables and setting them to the previously entered values, if those values exist. Then it outputs a simple form with a text field for the title and a textarea for the text content. Lastly you will define the update function. This function takes two parameters, $new_instance and $old_instance, and is responsible for updating your widgets with new options when they are submitted. Here you simply define $instance as an empty array. You then set the title and text keys to the $new_instance values if they exist. You then return $instance.
https://developer.wordpress.org/themes/functionality/widgets/
Reply
Anonymous
Information Epoch 1752371479
Think parallel.
Home
Notebook
Contact us