Instagram Shortcode in Hugo
Hugo it self has built-in Instagram shortcode supports, but for some reason it can’t be used anymore. In this article i will write about how to solve this problem in your Hugo environment.
Instagram Shortcode
If you are using Instagram shortcode in Hugo and having an issue with it, you are not the only one. Using Instagram shortcode with {{< instagram "BWNjjyYFxVx" "hidecaption" >}}
will generate an error like this:
|
|
At the moment, Hugo using deprecated oEmbed-legacy linked API endpoint. Those deprecated API causes an error when Hugo retrieving the data. This is also happen with the Hugo Continuous Integration build which force Erik to allow getJSON errors to be ignored with this commit.
Problem Solving
Facebook Developers lead us to use the newest Instagram oEmbed endpoint instead. This topic is being discused in #7879. Using the newest API is required to create a Facebook App to generate App ID and Client Token.
Facebook App Configuration
- Create an app in Facebook Developers Page
- Add Instagram Graph API and oEmbed to your facebook app
- Don’t forget to activate oEmbed plugin
- Find and copy App ID in top left corner and use it for
.Site.Params.oembed.appId
- Go to Settings > Advanced > Security
- Copy Client Token and use it for
.Site.Params.oembed.clientToken
Theme Configuration
After you get both App ID and _Client Token, follow these instruction below:
Create an
oembed.html
inYourProject/layouts/shortcodes
At this point we will integrate url query string parameter both Instagram Post and TV. The
$type
parameter withp
will stand for Instagram Post andtv
for Instagram TV. Theig
parameter will confirm that the oEmbed used is for Instagram.1 2 3 4 5 6 7 8 9 10
{{- $oe := .Site.Params.oembed -}} {{- $appId := $oe.appId -}} {{- $clientToken := $oe.clientToken -}} {{- if not $oe.privacy -}} {{ $host := .Get 0 }} {{ $type := .Get 1 }} {{ $id := .Get 2 }} {{ $hideCaption := cond (eq (.Get 3) "hidecaption") "1" "0" }} {{ with getJSON "https://graph.facebook.com/v11.0/instagram_oembed/?url=https://instagram.com/" $type "/" $id "/&hidecaption=" $hideCaption "&access_token=" $appId "|" $clientToken }}{{ .html | safeHTML }}{{ end }} {{- end -}}
Add front matter in
config.toml
:1 2 3 4
[params.oembed] appId = "YourAppId" clientToken = "YourClientToken" privacy = false
Create an example
instagram
input in your markdown:
a. Sample input of Instagram Post
|
|
b. Sample input of Instagram TV
|
|
- The rendered output will be like this:
a. Sample output of Instagram Post
b. Sample output of Instagram TV
At last, you can enjoy to use Instagram shortcode in Hugo without any problems. Have a good day! 😉