I am using the default WordPress embed to embed a couple of videos in the content.
I want to extract the video link from [embed]https://www.youtube.com/watch?v=Z9QbYZh1YXY[/embed]
, can anyone help with the regex?
I am using the default WordPress embed to embed a couple of videos in the content.
I want to extract the video link from [embed]https://www.youtube.com/watch?v=Z9QbYZh1YXY[/embed]
, can anyone help with the regex?
No need to use regex. WordPress has a handy function to strip shortcodes from a string.
Use strip_shortcodes( $content )
.
So in your case,
$content = "[embed]https://www.youtube.com/watch?v=Z9QbYZh1YXY[/embed]";
$your_clean _url = strip_shortcodes( $content );
echo $your_clean_url; // should output https://www.youtube.com/watch?v=Z9QbYZh1YXY
EDIT
Here's a function (tested) that will remove your "unwanted" shortcode, and any other one.
function remove_unwanted_shortcode($shortcode, $content) {
$start = "[" . $shortcode . "]";
$stop = "[/" . $shortcode . "]";
$cleanFirst = str_ireplace($start, '', $content);
$cleanSecond = str_ireplace($stop, '', $cleanFirst);
return $cleanSecond;
}
And you would use it like so:
$content = "[embed]https://www.youtube.com/watch?v=Z9QbYZh1YXY[/embed]";
$short = "embed";
echo remove_unwanted_shortcode($shortcode, $content);
if only want to display your video link you can put the link like
<a href="https://www.youtube.com/watch?v=Z9QbYZh1YXY">https://www.youtube.com/watch?v=Z9QbYZh1YXY</a>
(it will display your link like hyperlink )
[embed]https://www.youtube.com/watch?v=Z9QbYZh1YXY[/embed]
when I get $content = get_the_content()
. I want to remove those tags and only get the URL
– user23355
Sep 26 '17 at 10:44
if ( has_post_format( 'video' ) ) { $video_content = get_the_content(); }
and the ouput is[embed]https://youtube.com/sfsdfsf[/embed]
– user23355 Sep 26 '17 at 13:54