Notifications
Clear all

wpForo 1.x.x [Closed] Auto Embed Youtube Videos Responsive

3 Posts
1 Users
0 Reactions
1,209 Views
dimalifragis
Posts: 2611
Topic starter
(@dimalifragis)
Famed Member
Joined: 5 years ago

After struggling with it:

https://wpforo.com/community/general-discussions/original-wp-embed-function-for-youtube/paged/5/#post-62171

I was finally able to auto-embed Youtube Videos FULLY RESPONSIVE. So here is what i did:

1. Installed and Activated this tiny plugin:

https://wordpress.org/plugins/youtube-widget-responsive/

2. Changed the posted snippet of code by @Robert a little bit:

add_filter('wpforo_content_after', 'wpforo_custom_video_embed', 10);

function wpforo_custom_video_embed($content)
{
    
    $paterns = array();
    
    $paterns[] = "/<a[^><]+>\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i";
    
    $paterns[] = "/<a[^><]+>\s*[a-zA-Z\/\/:\.]*youtu.be\/([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i";
    
    $content = preg_replace($paterns, "[youtube video=\"//www.youtube.com/embed/$1\" ]", $content);
    
    return $content;
    
}

3. NOTHING ELSE is needed, since the css and iframe are handled by the plugin of (1)

It seems to work fine for me, please check it out yourself and let me know. Also it could be expanded or a better regex maybe. I'm not an expert. @tutrix is the master of all that. And @robert of cource.

I hope i have helped.

2 Replies
dimalifragis
Posts: 2611
Topic starter
(@dimalifragis)
Famed Member
Joined: 5 years ago

A little better code, remove some of Youtube branding like "watch it on Youtube and some controls".

Smoother for user.

 

add_filter('wpforo_content_after', 'wpforo_custom_video_embed', 10);

function wpforo_custom_video_embed($content)
{
    
    $paterns = array();
    
    $paterns[] = "/<a[^><]+>\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i";
    
    $paterns[] = "/<a[^><]+>\s*[a-zA-Z\/\/:\.]*youtu.be\/([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i";
    
    $content = preg_replace($paterns, "[youtube video=\"//www.youtube.com/embed/$1\" modestbranding=1 controls=0 ]", $content);
    
    return $content;
    
}
dimalifragis
Posts: 2611
Topic starter
(@dimalifragis)
Famed Member
Joined: 5 years ago

Updated, removed some uneeded code.

add_filter('wpforo_content_after', 'wpforo_custom_video_embed', 10);

function wpforo_custom_video_embed($content)
{

    $paterns = array();

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

    $paterns[] = "/<a[^><]+>\s*[a-zA-Z\/\/:\.]*youtu.be\/([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i";

    $content = preg_replace($paterns, "[youtube video=\"$1\" modestbranding=1 controls=0 ]", $content);

    return $content;

}