Embedding YouTube Video from Text Block
-
Hey!
I’m building a block, where You can paste YouTube URL and it’s embedding it via php. It works fine on frontend (previewed or actual page). But in the editing it’s displaying error when saving (it saves, but appears an error message on Gutenberg – “Updating Failed”).I want to cope with displaying this error.
I made a function to edit the data inserted by user in gutenberg to create an embed url.
<?php function yt_video_trim() { $org_url = block_field('wideo', false); $trim_url = trim($org_url); $embed_id = substr($trim_url, -11); return $embed_id; } ?> <div class="custom-block-video-wrapper"> <iframe class="custom-block-video" src="<?= 'https://youtube.com/embed/'.yt_video_trim(); ?>" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </div>
Thanks for help in advance,
have a great day!
-
Hi @riverz,
Thanks for letting us know.Could you please share a screenshot of what it looks like when “Gutenberg – “Updating Failed” displays?
Hm, what YouTube URL is it using?
With your block template, entering this URL worked fine:
https://www.youtube.com/watch?v=LezbkeV059Q
https://cldup.com/FlCEAS37mD.gif
- This reply was modified 3 years, 2 months ago by Ryan Kienstra.
Thanks for quick reply!
Screenshot: https://imgur.com/a/SFBmLAW
And yeah, plugins renders everything on preview(.php) and frontend(block.php) very well same as You tried! The problem is only with this red message from screenshot. Of my site editors informed me about a critical error too, but never got it myself – so I’m not focusing on it (it showed up probably because of some changes in block template between updating from old version to updated).
https://www.youtube.com/watch?v=U4dilE1eh9s
https://youtu.be/U4dilE1eh9s
Pasting each of mentioned links will work fine, I’m getting only video ID via substr(); and pasting value. That’s working well as You tried.
I wonder if I’m not doing something wrong with php. After quick research I got some info about problem with sanitizing – but I don’t wont to change any WP files in case of harming the security or something.
- This reply was modified 3 years, 2 months ago by riverz.
Hi @riverz,
Sorry for the delay, and thanks for your screenshot and information.Hm, the PHP in your template looks fine. This might be a hard one to debug.
1. If there’s a failed request in the DevTools > Network tab when that ‘Updating Failed’ error displays, could you please take a screenshot of it?
2. What is the field type that the URL is entered in?
Hi there,
thanks for another reply.Glad to see that’s everything right with php.
1. Screen:
https://imgur.com/a/GnRM1KI
This particular one appears only in this problematic case.2. Yeah, actually I forgot to change it from text to url. But after change nothing went better. Problem is still there.
Thanks
Hi @riverz,
Thanks for your screenshot.Hm, as you saw, there’s a PHP error somewhere.
If you have access to the server logs, it’d be interesting to see what the error was.
- The topic ‘Embedding YouTube Video from Text Block’ is closed to new replies.