Forum
Cannot get Youtube iframe into my code
Hi,
First off I'm using Perch 3.
I have followed the instructions here https://docs.grabaperch.com/addons/field-types/youtube/
I have added the youtube addon (It's not linked in the youtube doc), which I located in the perch2 addon archive and added to addons/fieldtypes/youtube
I have added an API key to the config file
My field is in a repeater for a carousel and the relevant section looks like this
<perch:if exists="video">
<li>
<perch:content id="video" type="youtube" label="YouTube URL" output="embed" />
</li>
<perch:else />
If I copy and paste an embed from youtube into the youtube field e.g. Youtube URL <iframe width="854" height="480" src="https://www.youtube.com/embed/T1R-d1ejlio" frameborder="0" allowfullscreen></iframe>
and save changes
the output in my source html is as follows <li> & l t ; iframe width="854" height="480" src="https://www.youtube.com/embed/T1R-d1ejlio" frameborder="0" allowfullscreen & g t ; & l t ;/iframe & g t ; </li>
If I just enter a youtube link instead of the full embed I just get https://www.youtube.com...
How do I get the iframe code in tag form? e.g.
<li>
<iframe width="854" height="480" src="https://www.youtube.com/embed/T1R-d1ejlio" frameborder="0" allowfullscreen></iframe>
</li>
There are no errors in my debug report.
Thanks
As a follow up I had the youtube addon in the wrong folder youtube/youtube/
Pasting the embed into the field I now get
Notice: Undefined index: width in C:\xampp\htdocs\quigstock\perch\addons\fieldtypes\youtube\youtube.class.php on line 113
Notice: Undefined index: height in C:\xampp\htdocs\quigstock\perch\addons\fieldtypes\youtube\youtube.class.php on line 114
and at the bottom of the debug in red
Array ( [type] => 8 [message] => Undefined index: height [file] => C:\xxx\htdocs\xxx\perch\addons\fieldtypes\youtube\youtube.class.php [line] => 114 )
In addition I do now get a video showing below the field, but clicking on it says An error occurred please try later.
Note: if I paste the embed code into a static test page the video works
The YouTube field type takes a YouTube video URL as the input, not an iframe. If you have the iframe already, it doesn't make sense that you'd want a special field type.
I have been trying both. I understand the logic in what you say.
If I use a Youtube video url I still get the same problem and error messages. Width and Height undefined line 113, 114
The only difference is that the video under the youtube url field now plays
If I change my template to (experimenting)
No difference same error.
Changing output to 'player' just outputs the link
What do you get on your web page?
I read in one of other threads that the problem might have had something to do with the poster's staging server.
I have copied everything to my development site folder dev.mysite.com. On the development site I have not yet upgraded from Perch: 2.8.32
It appears to be working with no errors. Video is present!
On my xampp localhost where I have upgraded to Perch 3
The video is present and working, but I am getting the following errors on the screen (repeated twice)
Notice: Undefined index: width in C:\xampp\htdocs\quigstock\perch\addons\fieldtypes\youtube\youtube.class.php on line 113
Notice: Undefined index: height in C:\xampp\htdocs\quigstock\perch\addons\fieldtypes\youtube\youtube.class.php on line 114
This is my machine detail page
My machines listing page
and my machine detail template
These errors also occur on the listings page for the category with the one product I have added video to, so possibly something up with my scripting/logic.???
I reverted back to Perch 2.8.32 on my xampp localhost server and the width and height errors were there, so it doesn't have anything to do with the Perch update. It works ok on-line, but does present an issue for testing locally. Not sure of the solution.
Have you checked your error log?
Thanks for your patience Drew, appreciated. I will check out my error log. I could be wrong, but the problem seems to be pointing at the Youttube API and my localhost server. I'm also using the likes of localhost:[port number] for my root addresses, so I'm not sure if that has something to do with it. APIs and the like are a bit over my head.
I checked the error log and nothing enlightening, just repeats of the lines below.
[Fri Jun 16 23:27:32.122963 2017] [:error] [pid 1728:tid 1972] [client 127.0.0.1:xxxxx] PHP Notice: Undefined index: width in C:\xampp\htdocs\quigstock\perch\addons\fieldtypes\youtube\youtube.class.php on line 113, referer: https://quigstock.dev/machines/turning/ [Fri Jun 16 23:27:32.122963 2017] [:error] [pid 1728:tid 1972] [client 127.0.0.1:xxxxx] PHP Notice: Undefined index: height in C:\xampp\htdocs\quigstock\perch\addons\fieldtypes\youtube\youtube.class.php on line 114, referer: https://quigstock.dev/machines/turning/
I put a var_dump($raw) into the get_process function in the youtube addons and the online version returned:
localhost returned
The youtube addon isn't receiving the embedded/iframe version just the original link.
I would really love to know a solution to this. Currently searching "Youtube API and localhost server", but struggling to find an answer.
edit: Have just tried using ngrok with my localserver and it's throwing the same 2 errors
Do you have curl enabled?
I thought that was going to be it, but yes it is enabled.
Out of my depth here, but get_details() never appears to be called.
Drew,
Forgive my ignorance, but couldn't $raw be checked inside get_processed, and if it doesn't match up (no 'title' key for instance) pass the youtubeID to get_details and merge the result?
Is that a very bad idea?
I know it doesn't necessarily get to the root of the problem.
That is a very bad idea, yes. That would mean you'd be querying the YouTube API for every time someone loads the page, which would be both super slow and would possibly push you past your API cap.
You have to remember Drew that sometimes you are talking to dummies. After barking up the wrong tree the penny has finally dropped.
I logged into perch and went to my form with debug on and clicked save changes on the item with the youtube link in and lo and behold in orange there was my youtube link with all the relevant information. In short everything is setup correctly now, API credentials etc, but it needed a refresh.
What I didn't realise was that perch processes the youtube links ahead of time i.e.when you click save changes, which ties in with your comment.
Thanks
Yes, that's done to keep your site loading quickly.