Laravel - Integrate oEmbed into TinyMCE
oEmbed is a kind of standard to retrieve 3rd party site info.
Example, in rich editor like TinyMCE, if want to embed
a YouTube video, what we usually do is
- Get the YouTube video embed code
- Click on the TinyMCE view source button
- Paste the embed code into the content
I did that previously, and it works. But when I do the same thing to Instagram
post, it doesn’t work so well, especially embed more than 1 post.
If you ever use Wordpress before, you will notice when you paste a YouTube link
directly to editor, and it will load the video, without the need of embed code.
Let’s add the oEmbed feature into your Laravel app
1. install a composer package
Because I couldn’t found any good JavaScript oEmbed plugin, then I decided to use
this PHP plugin.
1 | $ composer require embed/embed |
2. Create an API endpoint
Add a line to routes/web.php
1 | Route::get('oembed', ['as' => 'oembed', 'uses' => 'YourController@oEmbed']); |
In YourController.php
1 |
|
Now, you can test your endpoint
GET https://yoursite.com/oembed?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DG5seUqK1EeE
The most important data is the code.
3. Show the 3rd party content into TinyMCE
1 | function isValidURL(str) { |
Now, let’s try to paste a YouTube link to TinyMCE editor. It should works 😉