0

My previous blog author using his font-family and size on every sentence of blog post. I want apply new css rule to post but it cant overide these in-line styles.

I tried searching around and found this one:

add_filter( 'the_content', function( $content ){
  return str_replace( ' style="', ' data-style="', $content);
});

But it removing style of whole website. I just want remove style that used on single post's content only.

1

You want to remove the style from single post. And the "single post" term is confusing. Whether you need it for a single post or the single post template.

In either case you have to use your code with conditional and your code may look like this.

If you want to remove from all posts(not pages but including custom post types).

add_filter( 'the_content', function( $content ){

    if(is_single()){

        return str_replace( ' style="', ' data-style="', $content); 

    }

    else{

       return $content;

    }

}); 

If you want to remove from all posts(only WordPress post, not custom post type or pages).

add_filter( 'the_content', function( $content ){

    if (is_singular('post')) {

          return str_replace( ' style="', ' data-style="', $content);

    }  
    else{

       return $content;

    }
}); 

is_singular is the WordPress API conditional to check post types. https://developer.wordpress.org/reference/functions/is_singular/

If you want to remove from only one particular post.

add_filter( 'the_content', function( $content ){

    if(is_single([POST-ID])){

          return str_replace( ' style="', ' data-style="', $content);

    }
    else{

       return $content;

    }
}); 
| improve this answer | |
  • Follow your instruction. I added this code to my theme function.php. But it's not working if(is_single()){ add_filter( 'the_content', function( $content ){ return str_replace( ' style="', ' data-style="', $content); }); } – Hiển Hồ May 30 at 16:03
  • @HiểnHồ The conditionals will be inside the add_filter block. See the code, I have modified it above. Thank you. – Behemoth May 31 at 7:15
  • Thank you man. You really kind for helping me. But i tried you both above codes, its remove style of pages also. – Hiển Hồ May 31 at 12:26
  • Yes you are right. I have not checked the pages. I have modified the code and its working fine for me now. Tested on latest ver. – Behemoth Jun 2 at 4:21
  • so happy with my blog now. your code worked well. thank again Behemoth – Hiển Hồ Jun 2 at 14:58
1

You can try this code it works better

add_filter('the_content', function( $content ){
    $content = preg_replace('/ style=("|\')(.*?)("|\')/','',$content);
    return $content;
}, 20);
| improve this answer | |
  • But this one will replace all in-line style of pages also. I just want to remove styles of WordPress post only – Hiển Hồ May 30 at 16:06

Your Answer

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

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