AI Search
Classic Search
 Search Phrase:
 Search Type:
Advanced search options
 Search in Forums:
 Search in date period:

 Sort Search Results by:

Filter by custom fields

Topic prefix

AI Assistant
Notifications
Clear all

[Closed] Auto Embed Youtube Videos Responsive

3 Posts
1 Users
0 Reactions
2,667 Views
dimalifragis
Posts: 2600
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
(@dimalifragis)
Famed Member
Joined: 6 years ago
[#19756]

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: 2600
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
(@dimalifragis)
Famed Member
Joined: 6 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: 2600
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
(@dimalifragis)
Famed Member
Joined: 6 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;

}

 


Share: