The PHP function on this page supports the article published here, titled "Add Stock Quote Graphs to WordPress With Shortcode (Alpha Vantage API)". The PHP function retrieves stock data from the Alpha Vantage API and renders it into a Google graph. It's rather incomplete in that it only seeks to provide a single generic example of how the data might be used.
Find the WordPress shortcode article here.
The function requires use of Simple Cache.
PHP Function
1
<?php
2
include('../simple-cache/cache.inc.php');
3
4
/*
5
Add Stock Quote Graphs to WordPress With Shortcode
6
http://www.beliefmedia.com/stock-quote-graph-wordpress
7
*/
8
9
10
11
12
$atts = array(
13
'width' => '600',
14
'height' => '410',
15
'time' => '2',
16
'number' => '90',
17
'size' => 'compac', /* compac or full */
18
'interval' => '60', /* 1min, 5min, 15min, 30min, 60min */
19
'apikey' => 'xxxxxxxxxxxxxxxxx',
20
'cache' => 3600
21
);
22
23
/* Merge $args with $atts */
24
25
26
27
28
29
if ($cachedposts !== false) {
30
return $cachedposts;
31
32
} else {
33
34
switch ($atts['time']) {
35
case 1:
36
$series = 'TIME_SERIES_INTRADAY';
37
$series_name = 'Time Series (' . $atts['interval'] . 'min)';
38
break;
39
case 2:
40
$series = 'TIME_SERIES_DAILY';
41
$series_name = 'Time Series (Daily)';
42
break;
43
case 3:
44
$series = 'TIME_SERIES_DAILY_ADJUSTED';
45
$series_name = 'Time Series (Daily)';
46
break;
47
case 4:
48
$series = 'TIME_SERIES_WEEKLY';
49
$series_name = 'Weekly Time Series';
50
break;
51
case 5:
52
$series = 'TIME_SERIES_MONTHLY';
53
$series_name = 'Monthly Time Series';
54
break;
55
default:
56
$series = 'Time Series (Daily)';
57
break;
58
}
59
60
/* Get Stock data */
61
$data = @file_get_contents('https://www.alphavantage.co/query?function=' . $series . '&symbol=' . $symbol . '&interval=' . $atts['interval'] . 'min&apikey=' . $atts['apikey'] . '&interval=' . $atts['interval'] . 'min&outputsize=' . $atts['size']);
62
if ($data === false) return '<p>Data currently unavailable.</p>';
63
64
$data = $data[$series_name];
65
66
/* Return portion of results & reverse */
67
68
69
70
foreach ($data AS $key => $value) {
71
$chart .= ',[new Date(' . str_replace(array('-', ' ', ':'), ',', $key) . '), ' . $value['4. close'] . ']';
72
}
73
74
75
76
/* Build chart with fresh data */
77
$return = "<script type='text/javascript' src='https://www.gstatic.com/charts/loader.js'></script>
78
<script type='text/javascript'>
79
google.charts.load('current', {packages: ['corechart', 'line']});
80
google.charts.setOnLoadCallback(drawTrendlines);
81
82
function drawTrendlines() {
83
var data = new google.visualization.DataTable();
84
data.addColumn('date', 'Date');
85
data.addColumn('number', 'Close');
86
87
data.addRows([
88
$chart
89
]);
90
91
var options = {
92
hAxis: {
93
title: 'Date'
94
},
95
backgroundColor: 'transparent',
96
vAxis: {
97
title: 'Stock Price'
98
},
99
colors: ['#AB0D06'],
100
trendlines: {
101
// 0: {type: 'exponential', color: '#333', opacity: 1},
102
// 1: {type: 'linear', color: '#111', opacity: .3}
103
}
104
};
105
106
var chart = new google.visualization.LineChart(document.getElementById('chart_div_$interval'));
107
chart.draw(data, options);
108
}
109
</script>";
110
111
/* Chart container */
112
$return .= '<div id="chart_div_' . $interval . '" style="width: ' . $atts['width'] . 'px; height: ' . $atts['height'] . 'px;"></div>';
113
114
/* Set transient chart data */
115
116
return $return;
117
}
118
}
Usage
The function requires only a stock symbol. To alter the default attributes as defined by the $atts
array, pass them via an $args
array as a second function argument.
WordPress Shortcode Result
The result as returned from our shortcode function is as follows:
Download
Download the WordPress shortcode here.