Notifications
Clear all

[Solved] Original WP embed function for YouTube?

38 Posts
18 Users
8 Likes
20.5 K Views
Posts: 27
(@sodemieters)
Eminent Member
Joined: 5 years ago

If adding this code for Youtube.
I get error 500

 

Als deze code voor Youtube toevoeg.
Krijg ik error 500

Posts: 6
(@sylviajiji)
Active Member
Joined: 5 years ago

No encuentro dónde colocar el código... ¿Me pueden ayudar?

 

Robert
Posts: 10499
Admin
(@robert)
Support Team
Joined: 8 years ago

The code should be inserted in functions.php file of your theme. Here is some good instruction: How to Easily Add Custom Code in WordPress (without Breaking Your Site)

dimalifragis
Posts: 2563
(@dimalifragis)
Famed Member
Joined: 4 years ago

@Robert and @Tutrix

Could this code to auto embed youtube be Responsive?

 

Thanks

11 Replies
Tutrix
(@tutrix)
Joined: 4 years ago

Noble Member
Posts: 1247

@dimalifragis 

i don't use this code, but to make an iframe responsive you have to put a div container around it

> How To - Responsive Iframes

add_action( 'wp_head', function () { ?>
<script>
$(document).ready(function() {	
$('#wpforo #wpforo-wrap .wpforo-post-content iframe').wrap(' <div class="container"></div> ');
});
</script>
<?php } );

or test adding the div directly to the code

dimalifragis
(@dimalifragis)
Joined: 4 years ago

Famed Member
Posts: 2563

@tutrix That script needs an extra line to be accepted "<?php } );"

Also it doesn't seem to work at all.

But thanks !!!

Tutrix
(@tutrix)
Joined: 4 years ago

Noble Member
Posts: 1247

@dimalifragis 

ok, I use the snippet plugin that adds "<?php" automatically

you have to use the CSS code from the link above and the CSS class from the link too

 

> jquery wrap an iframe in div

dimalifragis
(@dimalifragis)
Joined: 4 years ago

Famed Member
Posts: 2563

@tutrix Thanks again, i was not able to make this to work, alas ...

dimalifragis
(@dimalifragis)
Joined: 4 years ago

Famed Member
Posts: 2563

@tutrix Could you please check the code that Robert posted in this topic above a bit?

I see a stray

</a>

in the result at the end. I mean using the unaltered code, just like Robert posted it.

Tutrix
(@tutrix)
Joined: 4 years ago

Noble Member
Posts: 1247

@dimalifragis 

remove

<a[^><]+>

and use

/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA- Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i

/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_] +)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i
dimalifragis
(@dimalifragis)
Joined: 4 years ago

Famed Member
Posts: 2563

@tutrix Thanks, it doesn't seem to work at all.

Tutrix
(@tutrix)
Joined: 4 years ago

Noble Member
Posts: 1247

@dimalifragis 

the code (without wpForo content) looks like this

function convertYoutube($string) {
    return preg_replace(
        "/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i",
        "<iframe src=\"//www.youtube.com/embed/$2\" allowfullscreen></iframe>",
        $string
    );
}
dimalifragis
(@dimalifragis)
Joined: 4 years ago

Famed Member
Posts: 2563

@tutrix I know. I found it on the net, still it doesn't work in our case.

dimalifragis
(@dimalifragis)
Joined: 4 years ago

Famed Member
Posts: 2563

@tutrix I tried all that in a staging site, no plugins, default theme and wpForo. it seems that even the original code provider some time ago from @robert, suffers from this stray </a>.

OR the code is ok and wpForo somehow adds that for some reason.

I hope that stray tag doesn't cause any issue (i think it doesn't).

And thanks again for your great community help here.

Tutrix
(@tutrix)
Joined: 4 years ago

Noble Member
Posts: 1247

@dimalifragis 

I can't test this unfortunately, I don't use the code as my WP theme has the YouTube feature integrated. 😉 

Page 5 / 5