Reputation: 280
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>';}
?>
Upvotes: 4
Views: 189
Reputation: 109
I would recommand you to hook on the_content
filter to check the current content if already has the wanted shortcode, if not, add it.
add_filter( 'the_content', 'func_53829055', 10 );
/**
* Check if the shortcode exists, if not, add it
*
* @param string $post_content
*
* @return string
*/
function func_53829055( $post_content ) {
// Add your custom conditions
if( ! is_single() && ! in_category( 1 ) ) {
return $post_content;
}
// Use a regex or strpos depending on the needs and post_content length/complexity
if ( false !== strpos( $post_content, '[yotuwp' ) ) {
// It already has the wanted shortcode, then return the post content
return $post_content;
}
// If add the video before post content
$before = '<h4 class="post-title entry-title">Video</h4>';
$before .= do_shortcode( 'my_shortcode' );
$before .= '<br />';
$post_content = $before . $post_content;
// If add the video after post content
$post_content .= '<br />';
$post_content .= '<h4 class="post-title entry-title">Video</h4>';
$post_content .= do_shortcode( 'my_shortcode' );
return $post_content;
}
Note that the priority has to be lower than 11, because shortcodes are replaced on 11 : add_filter( 'the_content', 'do_shortcode', 11 );
Upvotes: 0
Reputation: 491
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"]');
}
Upvotes: 5
Reputation: 230
Use file_get_contents, like this:
if (strpos(file_get_contents($_SERVER['REQUEST_URI']), 'youtube') === false) {
// you can add your video
}
Upvotes: 3