Skip to content

Instantly share code, notes, and snippets.

Last active May 1, 2016 16:45
What would you like to do?
Wordpress Statistics widget for the Dashing dashboard

Wordpress Statistics widget for the Dashing dashboard

Tracks all time total stats and views per post

Sample HTML to put in your .erb file:

    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
      <div data-id="wordpress_total_views" data-view="Number" data-unordered="true" data-title="Wordpress views" style="background-color: #21759b"></div>
      <i class="fa fa-wordpress icon-background"></i>
    <li data-row="1" data-col="1" data-sizex="3" data-sizey="1">
      <div data-id="wordpress_posts_views" data-view="List" data-unordered="true" data-title="Popular blog posts" style="background-color: #21759b"></div>
      <i class="fa fa-wordpress icon-background"></i>

Deploying to Cloud Foundry

If you are deploying to Cloud Foundry, you will need to create what's called a "user-provided" service. This is because of how environment variables are handled when involving special characters like $.

Create the user-provided service like this: cf cups wordpress_api -p '{"wordpress_api":"YOUR_TOKEN_HERE"}'

You will then get nice environment variables like these:

cf env appname

  "user-provided": [
    "credentials": {
     "wordpress_api": "YOUR_TOKEN_HERE"
    "label": "user-provided",
    "name": "wordpress_api",
    "syslog_drain_url": "",
    "tags": []

This way we can now get the correct API token without any issues.

#!/usr/bin/env ruby
require 'net/http'
require 'openssl'
require 'json'
# Created by Jonas Rosland,,
# Template used from
# This job tracks stats for your Wordpress blog
# This job should use the `Numbers` and `List` widgets
# One for the total of the blog and one for a list of the most popular posts
# Config
# ------
wp_host = ENV['WORDPRESS_HOST'] || ''
wp_site = ENV['WORDPRESS_SITE'] || ''
# The following line is only needed if you're deploying to Cloud Foundry
wp_bearer = JSON.parse(ENV['VCAP_SERVICES'], :symbolize_names => true)[:'user-provided'][0][:credentials][:wordpress_api]
# If you're deploying Dashing locally you can uncomment the following line
# number of posts to display in the list
max_length = 8
# order the list by the numbers
ordered = true
wp_period = 'year'
number_of_periods = 5
SCHEDULER.every '60m', :first_in => 0 do |job|
http =, 443)
all ="https://#{wp_host}/rest/v1.1/sites/#{wp_site}/stats/summary?period=#{wp_period}&num=#{number_of_periods}&pretty=true", initheader = {'Content-Type' =>'application/json', 'Authorization' => "Bearer #{wp_bearer}"})
posts ="https://#{wp_host}/rest/v1.1/sites/#{wp_site}/stats/top-posts?&period=#{wp_period}&max=#{max_length}&num=#{number_of_periods}&pretty=true", initheader = {'Content-Type' =>'application/json', 'Authorization' => "Bearer #{wp_bearer}"})
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # disable ssl certificate check
response_all = http.start {|http| http.request(all) }
response_posts = http.start {|http| http.request(posts) }
data_all = JSON.parse(response_all.body, :symbolize_names => true)
data_posts = JSON.parse(response_posts.body, :symbolize_names => true)
if response_all.code != "200"
puts "wordpress api error (status-code: #{response_all.code})\n#{response_all.body}"
posts_stats =
# Fix for Wordpress' crappy JSON formatting
first_day_of_year = data_posts[:days].flatten.first
data_posts[:days][first_day_of_year][:postviews].each do |post|
label: post[:title],
value: post[:views]
if ordered
posts_stats = posts_stats.sort_by { |obj| -obj[:value] }
send_event('wordpress_total_views', { current: data_all[:views] })
send_event('wordpress_posts_views', { items: posts_stats.slice(0, max_length) })
end # if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment