How-to and Troubleshooting
3
Posts
1
Users
0
Reactions
1,138
Views
Feb 24, 2022 8:42 pm
After struggling with it:
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
Feb 25, 2022 12:28 am
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; }
Feb 25, 2022 10:49 am
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; }