We have completed a task related to WordPress widgets recently, which brings us a chance to figured out how to create a new widget in WordPress. In this article, we will share our experience with you.
Step 1: Register Widget
It’s very convenient when WordPress provides a hook called widgets_init.
We will create “your_class_name” in the next step.
Step 2: Create class and structure of methods
Now we deal with a new class: “your_class_name”.
Since we finished this step, let’s go back to the widget management page and see the result.
Step 3: Select the form for your widget
In the form function, you can get a new form with available input types, such as text, checkbox, radio, and many others. The snippet below is an example:
Now you can modify the widget title easily.
Step 4: Update the form
It’s time to implement the update function.
When the user changes the title and clicks button save, the code above will run. You are able to save the user’s input in the database and send it to API or 3rd party services.
Step 5: Show data to front-end
Congratulation! You can display the data to front-end in the widget function now.
Simple, isn’t it? Hope your problems with WordPress widgets will be solved after following these guides. If you enjoy the article, please subscribe us for more useful WordPress tips.