6

I have a variable like this:

$post_content = get_the_content();

Here I want to remove some specific shortcode from the $post_content variable e.g I want to remove only this [video height="300" width="300" mp4="localhost.com/video.mp4"] all other shortcodes and content formatting should be leaved intact.

How could this be done?

UPDATE:

I'm able to remove some specific shortcode by using code something like this ...

<?php 
    echo do_shortcode( 
        str_replace(
            '[video height="300" width="300" mp4="localhost.com/video.mp4"]', 
            '', 
            get_the_content()
        ) 
    ); 
?>

But it is also removing all html tags/formatting of get_the_content(). How to avoid it?

3 Answers 3

4

If you want exactly this shortcode:

[video height="300" width="300" mp4="localhost.com/video.mp4"]

to output nothing, then you can use the wp_video_shortcode_override filter or the wp_video_shortcode filter to achieve that.

Here are two such examples:

Example #1

/**
 * Let the [video] shortcode output "almost" nothing (just a single space) for specific attributes
 */
add_filter( 'wp_video_shortcode_override', function ( $output, $attr, $content, $instance )
{  
    // Match specific attributes and values
    if( 
          isset( $atts['height'] ) 
        && 300 == $atts['height'] 
        && isset( $atts['width'] ) 
        && 400 == $atts['width'] 
        && isset( $atts['mp4'] )
        && 'localhost.com/video.mp4' == $atts['mp4']   
    )
        $output = ' '; // Must not be empty to override the output

    return $output;
}, 10, 4 );

Example #2

/**
 * Let the [video] shortcode output nothing for specific attributes
 */
add_filter( 'wp_video_shortcode', function( $output, $atts, $video, $post_id, $library )
{
    // Match specific attributes and values
    if( 
          isset( $atts['height'] ) 
        && 300 == $atts['height'] 
        && isset( $atts['width'] ) 
        && 400 == $atts['width'] 
        && isset( $atts['mp4'] )
        && 'localhost.com/video.mp4' == $atts['mp4']   
    )
        $output = '';

    return $output;
}, 10, 5 );

Note

I would recommend the first example, because there we are intercepting it early on and don't need to run all the code within the shortcode callback.

2
  • Nice approach. :-) Commented Sep 3, 2015 at 10:34
  • let's hope it works, since I must admit I didn't test it ;-) @PieterGoosen
    – birgire
    Commented Sep 3, 2015 at 10:41
1

You can use php preg_replace core function to search for your shortcode in content and remove it so your code may be :

$post_content_without_shortcodes = preg_replace ( '/\[video(.*?)\]/s' , '' , get_the_content() );
1
  • Thank you, this helped me remove a broken shortcode from the content that was carried over through an import. Commented Dec 9, 2019 at 22:56
0

Wouldn't it be

$post_content_without_shortcodes = strip_shortcodes(get_the_content());

or there's also

$post_content_without_shortcodes = remove_all_shortcodes(get_the_content());
1
  • No, i dont want to remove all shortcodes from the content. Just want to remove some specific shortcode having specific attributes. e.g i want to remove only this [video height="300" width="300" mp4="localhost.com/video.mp4"] all other shortcodes and content formatting should be leaved intact. Commented Jan 26, 2014 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.