I have a simple WP instance running on a vhost system. I always wondered why there is no preview when I share a link of my articles e.g. at facebook. I read that WP has oEmbed support, so I’m confused, because there is no line at the final resulting HTML source …
More confused, because it seems that you can just get your previews of you join the wordpress.com network?
In short: How can I enable/add oEmbed support to my own local blog and offer previews of my posts?
P.S. No, this is not about embeding other platforms content (yt, flickr, …) and get html previews on videos, images, … .
A self-hosted oEmbed provider: A simple Hello World demo
Here’s a very simple demo of a self hosted oembed provider (site A) that implements the json spec on oembed.com for rich type responses. Other available types are link, photo and video.
Site A:
This site is a self-hosted oembed provider that supports the json format and only implements the required oembed specs and skips the optional parameters.
We also implement
404
for urls not found and501
for formats not available.The key functions used here, are
url_to_postid()
,status_header()
,get_post()
andwp_send_json()
.Site B:
To test the oembed service provided by Site A, we must register it via:
on site B.
Then when we add a
site-a.tld
link in the editor we get:Before saving:
After saving:
Here
site-b.tld
will send the following GET request tosite-a.tld
:which gives the following 200 status response:
ps: I’ve not mentioned the auto oEmbed discovery.
Helpful resources:
The oEmbed specs.
The oEmbed Provider plugin (I’m not related to this one, but it helped me while writing this answer.)
If you want to know how to use the WordPress JSON REST API plugin by Ryan McCue, with the oembed provider, you might want to check out this blog post and this one (both great).
you can simply add the OG meta tags to your themes header.php in the section before wp_head():
I copied this from my themes header.php so you may have to change it to your needs.
Edit: you can see my themes header.php online at github: https://github.com/service4me/sandbox-wordpress-theme/blob/master/header.php
OG Title:
OG Type:
OG URL:
OG Image:
OG Description:
OG Locale: