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

Add Stock Quote Date Table to WordPress With Shortcode (Alpha Vantage API)

In early October we provided a shortcode that would query Alpha Vantage for stock data. One of the comments asked about how one would include the data in tables rather than a graph. This is a very crude example (I spend no more than a couple of minutes modifying it). If you're going to use it you'll want to style it and copy the inline CSS to the appropriate location.

■ ■ ■

Shortcode of [stocktable number="10"] returns the following. It's kind-of mobile responsive... so if you were to use it you would certainly need to make improvements. We're caching the data for three hours.

5th January 12:00
C: 8.6500
O: 8.4700
H: 8.7200
L: 8.4600
V: 70127787

6th January 12:00
C: 8.8400
O: 8.7900
H: 8.9400
L: 8.6800
V: 72590208

7th January 12:00
C: 9.0600
O: 8.9400
H: 9.0800
L: 8.8800
V: 77117101

8th January 12:00
C: 9.0000
O: 9.1000
H: 9.1400
L: 8.8900
V: 59162167

11th January 12:00
C: 9.3000
O: 8.8700
H: 9.3200
L: 8.8200
V: 96129537

12th January 12:00
C: 9.7800
O: 9.3000
H: 9.8200
L: 9.2903
V: 123938168

13th January 12:00
C: 9.7800
O: 9.9100
H: 9.9200
L: 9.6400
V: 89549250

14th January 12:00
C: 10.1700
O: 9.8100
H: 10.2000
L: 9.7900
V: 112695252

15th January 12:00
C: 9.8300
O: 10.0400
H: 10.0500
L: 9.8200
V: 99984954

19th January 12:00
C: 10.0200
O: 10.0600
H: 10.2100
L: 9.9700
V: 78732758

Original post published here.

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.

1
<?php 
2
/*
3
    Add Stock Quote Graphs to WordPress With Shortcode
4
    http://www.beliefmedia.com/stock-quote-graph-wordpress
5
    Response to question on website
6
*/
7
 
8
 
9
10
 
11
  $atts = shortcode_atts(array(
12
    'symbol' => 'F',
13
    'time' => '2',
14
    'number' => '90',
15
    'size' => 'compac', /* compac or full */
16
    'interval' => '60', /* 1min, 5min, 15min, 30min, 60min */
17
    'apikey' => 'xxxxxxxxxxxxxxxx',
18
    'cache' => 3600
19
  ), $atts);
20
 
21
 $transient = 'bmaast_' . md5(serialize($atts));
22
 $cachedposts = get_transient($transient);
23
 
24
 if ($cachedposts !== false) {
25
  return $cachedposts;
26
 
27
 } else {
28
 
29
    switch ($atts['time']) {
30
        case 1:
31
            $series = 'TIME_SERIES_INTRADAY';
32
            $series_name = 'Time Series (' . $atts['interval'] . 'min)';
33
            break;
34
        case 2:
35
            $series = 'TIME_SERIES_DAILY';
36
            $series_name = 'Time Series (Daily)';
37
            break;
38
        case 3:
39
            $series = 'TIME_SERIES_DAILY_ADJUSTED';
40
            $series_name = 'Time Series (Daily)';
41
            break;
42
        case 4:
43
            $series = 'TIME_SERIES_WEEKLY';
44
            $series_name = 'Weekly Time Series';
45
            break;
46
        case 5:
47
            $series = 'TIME_SERIES_MONTHLY';
48
            $series_name = 'Monthly Time Series';
49
            break;
50
        default:
51
            $series = 'Time Series (Daily)';
52
            break;
53
    }
54
 
55
    $return = '<style>
56
    * { 
57
        -moz-box-sizing: border-box; 
58
        -webkit-box-sizing: border-box; 
59
        box-sizing: border-box; 
60
    }
61
 
62
    .bmquotes {
63
        clear: both;
64
        padding: 0px;
65
        margin: 0px;
66
    }
67
 
68
    .col {
69
        display: block;
70
    float:left;
71
    margin: 5px 0 1% 1.6%;
72
    }
73
 
74
    .col:first-child {
75
        margin-left: 0;
76
    }
77
 
78
    .group:before,
79
    .group:after {
80
        content:""; display:table;
81
    }
82
 
83
    .group:after {
84
        clear:both;
85
    }
86
 
87
    .group {
88
        zoom:1;
89
    }
90
 
91
    .quote-container {
92
        width: 100%;
93
        display: block;
94
        margin: 0 auto;
95
    }
96
 
97
    .quote {
98
        width: 18.7%;
99
         padding: 5px;
100
    }
101
 
102
    .first {
103
        width: 18.7%;
104
        padding: 5px;
105
    }
106
 
107
    .title {
108
        width: 100%;
109
        padding: 5px;
110
    }
111
 
112
    @media only screen and (max-width: 480px) {
113
        .col {
114
            width: 100%;
115
            margin: 1% 0 1% 0%;
116
        }
117
 
118
        .quote {
119
            width: 100%;
120
        }
121
 
122
        .title {
123
            width: 100%;
124
            background-color: #C4C9C2;
125
            font-decoration: bold;
126
        }
127
 
128
    }
129
    </style>';
130
 
131
    /* Get Stock data */
132
    $data = @file_get_contents('https://www.alphavantage.co/query?function=' . $series . '&symbol=' . strtoupper($atts['symbol']) . '&interval=' . $atts['interval'] . 'min&apikey=' . $atts['apikey'] . '&interval=' . $atts['interval'] . 'min&outputsize=' . $atts['size']);
133
    if ($data === false) return '<p>Data currently unavailable.</p>';
134
    $data = json_decode($data, true);
135
    $data = $data[$series_name];
136
    if (empty($data)) return '<p>Data currently unavailable.</p>'; 
137
 
138
    /* Return portion of results & reverse */ 
139
    if ($atts['number'] != '') $data = array_slice($data, 0, $atts['number'], true);
140
    $data = array_reverse($data, true);
141
 
142
    /* Bulid table */
143
    foreach ($data AS $key => $value) {
144
 
145
       /* Use strtotime with $key (perhaps in the row) */  
146
       $return .= '<div class="title">' . date('jS F g:s', strtotime($key)) . '</div>';     
147
 
148
       $return .= '<div class="bmquotes group">
149
     <div class="col first">C: ' . $value['4. close'] . '</div>
150
     <div class="col quote">O: ' . $value['1. open'] . '</div>
151
     <div class="col quote">H: ' . $value['2. high'] . '</div>
152
     <div class="col quote">L: ' . $value['3. low'] . '</div>
153
     <div class="col quote">V: ' . $value['5. volume'] . '</div>
154
      </div>';
155
 
156
    }
157
 
158
   /* Set transient chart data */
159
   set_transient($transient, $return, $atts['cache']);
160
   return $return;
161
 }
162
}
163
add_shortcode('stocktable', 'beliefmedia_alpha_vantage_quotes');

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.

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