How can I create my own plugin in WordPress that would change the color of only the word ''hi'' and put it in bold in the article?

Viewed 18
add_filter( 'the_content', 'change_color' );

function change_color( $content ) {
    $source = array( '/(\b[hi])/i' );
   
    $result = preg_replace('/(\b[hi])/i', 
'<span style="color:red;"></span>', $result,$source,$content);
  

    return $result;
}
1 Answers

I am not sure if it will work on not but you can test it and see if does the job or not.

function custom_replace_hi_with_span_with_color( $content ) {
    if ( is_admin( ) ) {
        return $content;
    }

    $content = preg_replace( '/(\bhi\b)/', '<span style="color:red;">$0</span>', $content );

    return $content;
}
add_filter( 'the_content', 'custom_replace_hi_with_span_with_color', 99 );

Note: make sure you have site backup and FTP access to fix any error that might come after adding this custom code.

Related