Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check requested url for a text pattern then do something

I am using the code below to execute video shortcode on my WordPress website but some pages already contain manually added video which will cause duplicate when i use the code.

How can include a check if the page already contain a YouTube embedded iframe or video link and exclude pages which already have videos, here is what i have below:

if (is_single() && in_category(1) ) 
{
 echo '<h4 class="post-title entry-title">Video</h4>' ;
 echo do_shortcode( '[yotuwp type="keyword" id="'.get_post_field( 'post_title', $post_id, 'raw' ).'" player="mode=large" template="mix" column="1" per_page="1"]' );
}

I want to include youtube link check here:

 if (is_single() && in_category(1)

Here is what i am able to find Here but this scans the requested url instead of the content on it:

<?php
  if (stripos($_SERVER['REQUEST_URI'],'tout') == true && stripos($_SERVER['REQUEST_URI'],'dedans') == true) 
    {echo '<div class="clear"></div><a href="http://www.example.com/cakes/" class="btn"> >> View all Cakes</a>';}
?>
like image 740
user3476168 Avatar asked Jan 27 '23 00:01

user3476168


1 Answers

Since you already have the $post_id I suggest you get the Post object and do a regular expression match for 'youtube' or the short URL version 'youtu.be'. See sample code:

$post = get_post($post_id);
$content = apply_filters('the_content', $post->post_content);

if (is_single() && in_category(1) && !preg_match('/youtu\.?be/', $content)) {
    echo '<h4 class="post-title entry-title">Video</h4>';
    echo do_shortcode('[yotuwp type="keyword" id="' . get_post_field('post_title', $post_id, 'raw') . '" player="mode=large" template="mix" column="1" per_page="1"]');
}
like image 194
Solomon A. Avatar answered Jan 30 '23 04:01

Solomon A.