RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (6.59%*) • Fixed: 5.39% (6.59%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.48%*) • Investment PI: 5.39% (6.59%*)

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.

25th October 12:00
C: 11.0700
O: 11.2800
H: 11.3300
L: 11.0150
V: 39120742
28th October 12:00
C: 11.3700
O: 11.1650
H: 11.3900
L: 11.1212
V: 97967008
29th October 12:00
C: 10.4100
O: 10.5300
H: 10.5500
L: 10.1800
V: 156227983
30th October 12:00
C: 10.4700
O: 10.3950
H: 10.5300
L: 10.3300
V: 48853742
31st October 12:00
C: 10.2900
O: 10.4300
H: 10.4800
L: 10.2400
V: 72098705
1st November 12:00
C: 10.2200
O: 10.3300
H: 10.4100
L: 10.2000
V: 43472278
4th November 12:00
C: 10.3600
O: 10.2300
H: 10.5700
L: 10.2300
V: 55323849
5th November 12:00
C: 10.6000
O: 10.3299
H: 10.6400
L: 10.3000
V: 49308968
6th November 12:00
C: 11.1900
O: 10.9500
H: 11.2300
L: 10.8350
V: 100781772
7th November 12:00
C: 10.9600
O: 11.0050
H: 11.1100
L: 10.8300
V: 55407938

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
  $atts = shortcode_atts(array(
11
    'symbol' => 'F',
12
    'time' => '2',
13
    'number' => '90',
14
    'size' => 'compac', /* compac or full */
15
    'interval' => '60', /* 1min, 5min, 15min, 30min, 60min */
16
    'apikey' => 'xxxxxxxxxxxxxxxx',
17
    'cache' => 3600
18
  ), $atts);
19
 
20
 $transient = 'bmaast_' . md5(serialize($atts));
21
 $cachedposts = get_transient($transient);
22
 
23
 if ($cachedposts !== false) {
24
  return $cachedposts;
25
 
26
 } else {
27
 
28
    switch ($atts['time']) {
29
        case 1:
30
            $series = 'TIME_SERIES_INTRADAY';
31
            $series_name = 'Time Series (' . $atts['interval'] . 'min)';
32
            break;
33
        case 2:
34
            $series = 'TIME_SERIES_DAILY';
35
            $series_name = 'Time Series (Daily)';
36
            break;
37
        case 3:
38
            $series = 'TIME_SERIES_DAILY_ADJUSTED';
39
            $series_name = 'Time Series (Daily)';
40
            break;
41
        case 4:
42
            $series = 'TIME_SERIES_WEEKLY';
43
            $series_name = 'Weekly Time Series';
44
            break;
45
        case 5:
46
            $series = 'TIME_SERIES_MONTHLY';
47
            $series_name = 'Monthly Time Series';
48
            break;
49
        default:
50
            $series = 'Time Series (Daily)';
51
            break;
52
    }
53
 
54
    $return = '
55
<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
';
132
 
133
    /* Get Stock data */
134
    $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']);
135
    if ($data === false) return 'Data currently unavailable.';
136
    $data = json_decode($data, true);
137
    $data = $data[$series_name];
138
    if (empty($data)) return 'Data currently unavailable.';
139
 
140
    /* Return portion of results & reverse */
141
    if ($atts['number'] != '') $data = array_slice($data, 0, $atts['number'], true);
142
    $data = array_reverse($data, true);
143
 
144
    /* Bulid table */
145
    foreach ($data AS $key => $value) {
146
 
147
       /* Use strtotime with $key (perhaps in the row) */
148
       $return .= '<div class="title">' . date('jS F g:s', strtotime($key)) . '</div>';
149
 
150
       $return .= '<div class="bmquotes group">
151
<div class="col first">C: ' . $value['4. close'] . '</div>
152
<div class="col quote">O: ' . $value['1. open'] . '</div>
153
<div class="col quote">H: ' . $value['2. high'] . '</div>
154
<div class="col quote">L: ' . $value['3. low'] . '</div>
155
<div class="col quote">V: ' . $value['5. volume'] . '</div>
156
</p></div>';
157
 
158
    }
159
 
160
   /* Set transient chart data */
161
   set_transient($transient, $return, $atts['cache']);
162
   return $return;
163
 }
164
}
165
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.

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment