I have a WordPress site that I’m working on, which has a large section of the site that describes plant species. Each plant name begins with “R.”, and is followed by the species name. For example, one instance is “R. adenogynum.” All of these names must be italicized.
There are many instances of this, and to do it by hand would be very time-consuming. I’m trying to write a plugin that will check/italicize all the words in $content, each time “the_content” is run. In other words, I’m using an
add_filter( 'the_content', 'italicize' )
function italicize($content) {
...
function to do this.
I’ve tried to use the explode method like so:
$words = explode( ' ', $content );
and then checking each word in the $words array, but the problem is that whitespace and HTML tags also are part of $content, and this jumbles everything. Plus I would have to use implode to put everything back together, which is a bit messy as well.
So how can I edit these plant names successfully with a plugin?
Try this plugin:
http://urbangiraffe.com/plugins/search-regex/
It allows you to do a regex search and search/replace for your phrase.
Use regular expressions syntax.
This will match any phrase that begins with “R.” followed by a space, followed by an all lowercase word and replace it with an italicized version of that word.
This is a job best suited to preg_replace.
Your function would look something like this:
Demo of the php code, and Regular Expression tester