how to use preg_replace for wordpress video?

I am trying to format VIDEO of my wordpress blog for my amp pages. The code I am using in content editor is:

I want to display this video in AMP pages in this format:

<amp-video controls
width=”640″
height=”360″
layout=”responsive”>
<source src=”https://mywebsite.com/wp-content/uploads/20**/0*/video-name.mp4″ />
</amp-video>

I tried following functions but they didn’t worked for me.

Try 1:

function am_video_amp_format($content){
global $post;
$pattern =”~<video(.*?)></video>~i”;
$replacement = ‘<amp-video controls
width=”640″
height=”360″
layout=”responsive”>
<source src=”$2″ />
</amp-video>’;
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter(‘the_content’, ‘am_video_amp_format’);

Try 2:

function am_video_amp_format($content){
global $post;
$pattern =”~<iframe(.*?)>(.*?)<video(.*?)src=”(.*?)”>(.*?)</video>(.*?)</iframe>~i”;
$replacement = ‘<amp-video controls
width=”640″
height=”360″
layout=”responsive”>
<source src=”$4″ />
</amp-video>’;
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter(‘the_content’, ‘am_video_amp_format’);

Try 3:

function am_video_amp_format($content){
global $post;
$pattern =”~<video(.*?)><source(.*?)src=”(.*?)”(.*?)></video>~i”;
$replacement = ‘<amp-video controls
width=”640″
height=”360″
layout=”responsive”>
<source src=”$2″ />
</amp-video>’;
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter(‘the_content’, ‘am_video_amp_format’);

Also can I get height and width also?

Thank You!

Read more here:: how to use preg_replace for wordpress video?

Leave a Reply

Your email address will not be published. Required fields are marked *