How to Write a "Most Popular By Views" WordPress Plugin


This Cyber Monday Envato Tuts+ courses will be reduced to just $3. Don't miss out.

As you continue writing for WordPress more and more, the level of difficulty and complexity of your plugins will eventually reach far beyond the simple back-end data manipulation, as demonstrated in our beginner's article. In this installment we'll cover in-depth: database interaction, Admin Widgets, and Sidebar Widgets. Today's example will be a custom built "Most Popular by Views" plugin, which creates an admin area widget for us to view, and a sidebar widget for visitors to see.

Tutorial now on Wptuts+

Step 0. What it's Gonna Do?

Before we open our IDE or write any code, you should always write out a list of what features your plugin will and will not have (at least in its initial phase, you can always extend later). For our plugin, "Most Popular By Views," our primary feature is to display a list of the most popular posts (we'll say, top 10). Secondly, we'll also put the list in the admin area as a kind of low-tech analytics. Lastly, we'll give the developers the option to display it anywhere by making it available as a sidebar widget, and via a simple function.


  • Creates a list of the top ten most popular posts based on views
  • List is displayed in Admin area as a low-tech analytics
  • List is available as both a sidebar widget and function
The finished widgets

Step 1. The Database

Before we can display the most popular posts, we need to gather data about which posts are being viewed and how often, but even before we can do that, we need somewhere to put all that data. Inside of a new plugin file, let's create a new function (ppbv_create_table) that will check to see if our table exists. If it does not, we'll create it. Our method of doing this is rather simple; we'll use $wpdb->get_results to run a SQL statement that checks for a table named "wp_popular_by_views." If it doesn't find the table, get_results will return null. If that's the case, we'll use $wpdb->query to write in the new table. We run this function every time the plugin is activated.

Step 2. Catching the Data

The next thing we need to do, now that we have a table to store our data, is catch our data. We'll create a new function (ppbv_page_viewed) that we'll attach to the wp_head hook so that it'll run on every page load. Inside this function, we're going to do one of two things, after we check to see if the current page is already in the database: increase its views by one or, if it's not in the database, add it to the database. To figure out if the current page is already in the database, we're going to be using the $post object to get the "post_ID." This step is actually really simple, because we aren't collecting any complicated data; the comments in the code provide a detailed step by step in this process.

Database and views process

Step 3. Creating the Admin Widget

Next up, we're going to use this data we just added to our database to create an ordered list inside of an Admin Area Dashboard Widget. This process involves two functions: the first (ppbv_admin_widget) to generate everything inside the widget, and second, (ppbv_add_admin_widget) to create the widget itself. Let's start with the content function, pppbv_admin_widget. Firstly, since we're making an ordered list, let's echo out the opening tags for that. Then we'll globalize the $wpdb and $ppbv_tablename vars so we can access the database and retrieve the ten most viewed post's IDs. Then we'll run the returned array through a foreach statement, and use each individual ID to build a list-item and create a link to that page while also printing out its views (formatted with number_format to make it easier to read).

Now that we're generating content, let's create the widget. Inside the creation function, ppbv_add_admin_widget, we're going to call another function native to WordPress: wp_add_dashboard_widget. All we need to do is give wp_add_dashboard_widget the following parameters: [id of the container], [title in the container], [content function] (ours fills as such: 'popular_by_views', 'Most Popular Posts by Views', 'ppbv_admin_widget'). The last thing we need to do is attach our creation function to the wp_dashboard_setup hook.

And now we have a working dashboard widget for administraitors to see.

Admin Dashboard Widget

Step 4. Creating the Sidebar Widget

Creating a sidebar widget is fairly painless; unfortunately, certain parts aren't documented well (like how to make them uniform), but we'll cover that. Let's start by writing a new function (ppbv_display_widget) and, inside of it, we'll carbon copy our content function from the admin widget (I suggest moving the global calls to the top, outside the <ol> echo, of the function for clarity later on). Then the next step is to register our widget in WordPress via wp_register_sidebar_widget (parameters are: [id of the container],[title on the widget page],[content function] || 'popular_by_views', 'Most Popular Posts by Views', 'ppbv_display_widget').

This is actually the bare-minimum you need to do for a sidebar widget, but what 90% of all dynamic sidebars have certain settings applied about how widgets are styled, so let's conform our widget to those settings. The first thing we need to do is add in a parameter to our ppbv_display_widget function, $args. By adding this parameter inside the function, using the extract function on it, we gain access to several variables which will allow our plugin to be styled the same as the rest of the widgets in the sidebar.

"We don't need to provide any input for $args, WordPress will do that for us."

Thanks to $args and extract, we now have access to the following variables that we can echo to style our widget:

  • $before_widget
  • $after_widget
  • $before_title
  • $after_title
Sidebar Widget

Step 5. The Non-widget Function

Not everybody who uses this plugin is going to want to use the widget, so it's imperative that we provide them with another method of displaying our list. Thankfully, that's as simple as cloning our ppbv_display_widget function and removing the widget parts, replacing them with standard hardcoded HTML.


That's it guys, you've successfully made your very own "Most Popular by Views" WordPress plugin. Not too hard, was it?

When you've mastered the art of WordPress plugin development, stop by and begin selling your plugins for profit!

View Final Source Code with Commenting?