CMS Mastering Development Wordpress

Replace word in “the_content” only for index.php

I have some posts with video tag that contains autoplay (they should autoplay when you access the post page).

I am using the the_content to show these videos on index page.

The problem is that when you access the home page, all the videos start playing.

I have used the following function to remove it:

function replace_ap($text){
    $replace = array(
        'autoplay=""' => ''
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
add_filter('the_content', 'replace_ap');

but now the "autoplay" attribute is removed from the post pages also.

How can I remove it only from the index ?

My thought was to create a function that gets the content and add the filter to this function, then call it from index.php instead of "the_content", but I don’t know how or if it’s possible

Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *