Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

Using Google Graphs To Draw PHP, MySQL, and WP Version Usage (WordPress API)

While there's a number of organisations that dedicate resources to determining the breakup of PHP's version usage, WordPress maintain their own statistics. Based on their massive coverage, WP's broad reach provides a scientifically and statistically sound source of version data. This article will provide the necessary shortcode to retrieve the WP data and render it into a Google pie chart. The shortcode can also be used to retrieve MySQL and WordPress version usage. See graphs as we've used them on our reference page.

We generally use our own graphing utilities but we'll often revert back to Google charts for more sophisticated renderings.

The Result

The result of the shortcode [phpversions] is as follows:

PHP is said to be used by over 82% of all the websites whose server-side programming language can be identified.

At the time of writing, version 5.6 tends to dominate the space with the build representing nearly 40% of all installations.

To generate a donut chart representing WordPress version usage, use a piehole value between 0 and 1 as an attribute (0 is the default, meaning no hole). We'll also specify a name to avoid element ID conflicts and provide a title. For example, [phpversions piehole="0.4" type="wordpress" name="wp_piehole" title="WordPress Version Usage (source: WordPress)"]. The result:

For the last example we'll render the MySQL chart in 3d. Shortcode of [phpversions name="mysql_threed" type="mysql" 3d="true" title="MySQL Version Usage (source: WordPress)"] returns:

WordPress Shortcode

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

1
<?php 
2
/*
3
 Using Google Graphs To Draw PHP, MySQL, and WP Version Usage (WordPress API)
4
 http://www.beliefmedia.com/pie-graph-php-versions
5
*/
6
 
7
8
 
9
  $atts = shortcode_atts(array(
10
    'type' => 'php', /* php, mysql, wordpress */
11
    'name' => 'php_piechart',
12
    'title' => 'PHP Versions (source: WordPress)',
13
    'width' => 600,
14
    'piehole' => 0,
15
    '3d' => 'false',
16
    'height' => 400,
17
    'cache_temp' => 800,
18
    'cache' => 3600 * 24 * 14
19
  ), $atts);
20
 
21
  $transient = 'bmphp_' . md5(serialize($atts));
22
  $dbresult =  get_transient($transient);
23
 
24
  if ($dbresult !== false) {
25
  $result = $dbresult;
26
 
27
  } else {
28
 
29
     /* Get data from WordPress */
30
     $data = @file_get_contents('https://api.wordpress.org/stats/' . $atts['type'] . '/1.0/');
31
 
32
     if ($data != false) {
33
 
34
      $data = json_decode($data, true);
35
 
36
      /* Data */
37
      $title = $atts['title'];
38
      $version_data = "['$title', 'Percentage'],";
39
 
40
      foreach ($data AS $key => $value) {
41
        $version_data .= "['V$key', $value],";
42
      }
43
 
44
      $version_data = rtrim($version_data, ',');
45
 
46
      $result = '
47
      <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
48
      <script type="text/javascript">
49
        google.charts.load("current", {packages:["corechart"]});
50
        google.charts.setOnLoadCallback(drawChart);
51
        function drawChart() {
52
          var data = google.visualization.arrayToDataTable([
53
            ' . $version_data . '          ]);
54
 
55
          var options = {
56
            title: \'' . $atts['title'] . '\',
57
            is3D: ' . $atts['3d'] . ',
58
            pieHole: ' . $atts['piehole'] . ',
59
            backgroundColor: {
60
              fill: \'transparent\'            }
61
          };
62
 
63
          var chart = new google.visualization.PieChart(document.getElementById(\'' . $atts['name'] . '\'));
64
          chart.draw(data, options);
65
        }
66
      </script>';
67
 
68
      $result = '<div id="' . $atts['name'] . '" style="width: ' . $atts['width'] . 'px; height: ' . $atts['height'] . 'px;  margin: 0px auto;">' . $result . '</div>';
69
 
70
      /* Return and cache .. */
71
      set_transient($transient, $result, $atts['cache']);
72
 
73
      } else {
74
 
75
      /* Data temporarily unavailable */
76
      $result = 'Chart currently unavailable';
77
 
78
      /* Return and cache .. */
79
      if ($atts['p'] !== false) $result = '' . $result . '';
80
      set_transient($transient, $result, $atts['cache_temp']);
81
 
82
    }
83
 
84
  }
85
 
86
 return $result;
87
}
88
add_shortcode('phpversions', 'beliefmedia_php_version_google_pie_chart');

Shortcode Attributes

type

The type of chart: php, mysql, or wordpress.

name

The name of the chart. This is the div ID so isn't visible in your browser.

title

The chart title. Include WordPress as a source.

width

The chart width.

height

The chart height.

piehole

If you want a donut chart use a piehole value higher than 0 and less than 1. A value of piehole="0.4" seems to work well. A value of 0 will omit the piehole completely.

3d

If you want your chart in 3D, use 3d="true".

cache_temp

If the WP API is unavailable, we'll wait this long before making another request.

cache

The period we'll cache WP data. Defaults to 14 days.

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

PHP Function

A PHP function to be used outside of WordPress is available for download below. Usage requires our Simple Cache. Arguments are passed to the function as an array. Default usage renders a PHP chart.

Considerations

  • This shortcode was provided just to demonstrate example usage of live data. Consider it in the marketing realm to measure the success of certain campaigns, clicks against campaign URLs or individuals, sales performance etc.
  • When the 3d chart is rendered the piehole feature is unavailable.
  • A large number of pie chart features are available. Details here

Download


Title: Using Google Graphs To Draw PHP, MySQL, and WP Version Usage
Description: Displays PHP, MySQL, and WP version usage in a Google Pie Chart. Data sourced from WordPress.
  Download • Version 0.1, 2.0K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (1.2K)    PHP Code & Snippets, (1.3K)    

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  AUS Eastern Standard Time (Washington)

  Want to have a chat?
 

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment

READY TO HAVE A CHAT? CALL US ANYTIME ON 1300 235 433