Developing a WordPress Dashboard Widget Using Forminator
Unlike other form plugins, Forminator’s API is not blocked off by a pricey paywall, but available to everyone. This opens up endless possibilities for customization and opportunities to create unique applications and extensions using Forminator.
Better still, with Forminator as the foundation, you as a third party developer, can create custom business solutions for your clients that you can then sell on the open market for additional revenue streams. There is a lot of value waiting to be unlocked within the Forminator API.
In this post, I’m going to show you how to utilize the API to build a simple dashboard plugin in WordPress; however, what we’re building is not focus of this tutorial. My goal is to use the building of this simple plugin as a way to teach you how to approach this on your own. I hope you’ll walk away with some ideas once you see what is available to you.
Teach a Man to Use an API and He’ll Create Extensions for a Lifetime
The Forminator API supports CRUD (create, read, update, delete) operations on forms, polls, quizzes and their respective entries.
Here’s some of the methods available:
This is not an exhaustive list. The API documentation outlines all the available methods that you can use to build your own plugins.
For the purpose of this tutorial, we’re going to be building a custom widget for the WordPress dashboard that will show the most recent entries using the Forminator API. We’re going to be retrieving entries to display them in a widget, but you could also display them on a page or in a post with some modifications.
We’re going to start by creating a plugin in WordPress. Since this is a Forminator Add-On, we only want the plugin to run if Forminator is active, so we’re going to use the ‘forminator_loaded’ action and only set up our plugin if that action is called. This is important to include in your custom add-ons as well, but of course, you’ll name the function something appropriate for your unique situation.
Setting Up the Dashboard Widget
Now we can move on to creating the dashboard widget, but you can choose your own adventure here. Depending on what kind of plugin you’re creating, you might not need to create a dashboard widget. You’ll create something else instead.
For this tutorial, we’re going to use wp_dashboard_setup to add the widget to the WordPress dashboard page. We’ll load our widget after the wp_dashboard_setup hook is called.
In the add_forminator_dash_widget function below, we’ll instantiate the Forminator_Submissions_Dash_Widget class, which we’ll create next.
Creating the Dashboard Widget Class
The following is from the WordPress codex:
All the functions in your Plugin need to have unique names that are different from functions in the WordPress core, other Plugins, and themes. For that reason, it is a good idea to use a unique function name prefix on all of your Plugin’s functions. A far superior possibility is to define your Plugin functions inside a class (which also needs to have a unique name).
With that said, we’re going to take the far superior route and start by creating a unique plugin class called Forminator_Submissions_Dash_Widget. Within the class we’ll store the class instance, identify what form ID to retrieve submissions from and enter how many submissions we’d like to display.
I’m closing this code block with a bracket so that you won’t break your site if you copy and paste this in. Keep in mind that the code blocks in the remainder of this tutorial belong inside the bracket. If you want to use this plugin on your site, I recommend getting the code in it’s entirety on GitHub, instead of copying and pasting each bit.
In order to instantiate the plugin class, you’ll need to get the class instance.
1.6 million WordPress Superheroes read and trust our blog. Join them and get daily posts delivered to your inbox – free!
We will then declare our constructor method (it is empty as there’s no prerequisites needed) and register the dashboard widget.
We don’t want to show our widget to all our WordPress users, so we’ll add user_allowed() to check if the user is allowed to view the widget.
Then we’re going to set option defaults as a fallback in case our widget isn’t configured. We’ll be creating a configuration box in the next section.
If the user is not allowed to view the widget, we’ll display a message, otherwise we’ll get the submissions.
Configuring the Widget
In order for the user to be able to configure the widget, we’re going to add a configuration box that looks like this.
To do that, you’ll add the configure() method along with update and get options to make it possible to configure the widget.
Let’s start with the configure() method:
For the update, we need to fetch all dashboard widget options from the database, and create an array that merges the old options with the new ones.
Next, we’ll retrieve our widget options from the database.
Forminator to the Rescue
And now the part you’ve all been waiting for, populating the dashboard widget using the Forminator API.
To get the submissions, we’ll use Forminator_API::get_form() and Forminator_API::get_form_entries(). We’ll also check if we’ve set the form ID whose entries we want to display. If this is not set, we will prompt the user to properly configure the dashboard plugin.
We will also confirm that the form loaded successfully and render the submissions table.
All of this will go into the get_submissions() method, like this:
Now that we have our form and submission data, we need to create our markup render method. For this, we’ll use render_form_submissions(). We’ll display the data in an HTML table and showed the number of entries we specified earlier.
Your new widget is now fully functional and will retrieve submissions data using the Forminator API!
If you’re interested in further customizing your WordPress admin area, check out this guide on customizing the WordPress back end and this one on creating a marketing dashboard in WordPress.
If you’d like to use this widget on your site, you can get the all the code for the Forminator Dashboard Widget on GitHub.
Hasta La Vista, Baby
The dashboard widget was created for as a simple demo for this tutorial. This is only the beginning of what Forminator is capable of. Dive in to the Forminator API and see what you can come up with.
You can get Forminator completely free here.
Interested in learning more about development? Start with our guide on how to write functions.
We can’t wait to see what cool things you build on the Forminator framework.