0

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?

  • how you are getting your content ?? Using loop or using a wp function ?? – Harry Sep 26 '17 at 12:07
  • @Harry I am using the loop. Inside the loop: 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
0

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);
  • 1
    Than you so much @samuel-asor... That's clean and does the job very nicely. I owe you one. – user23355 Sep 27 '17 at 4:56
  • Glad to be of help. – Samuel Asor Sep 27 '17 at 5:30
  • One more thing, please mark the answer as accepted... :) – Samuel Asor Sep 28 '17 at 6:58
0

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 )

  • This is not what I mean. I have [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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy