WordPress Tutorial: Replace HTML Tags within the Content

Sure, I can think of at least half a dozen other things to blog about when it comes to WordPress right now, but I can’t pass this up.

Earlier today I ventured on the WordPress.org Help Forums. Since I am no where near efficient in writing PHP code for core, I figure I could lend a hand to those support threads that don’t get touched. Here is the backstory:

User has moved their entries from Blogger into WordPress. For some reason instead of using paragraph tags, this user has been using Div tags. The user’s posts are coming out all funky with spacing being an issue. What can be done?

At first glanced I wondered if by just editing each post by removing the Div tags could solve the problem. There are too many posts. What else can we do? Create functions!

I found this support post where line breaks were needing to be replaced by paragraph tags. That seemed to work, so I made some posts using Div tags in a local install and went to work.

What we need to do first is to convert all the div tags in the_content() to paragraph tags.

1
2
3
4
5
// Replaces all <div> in the_content with <p>
function clear_replacedivopen($content){
	return str_replace("<div>","<p>", $content);
}
add_filter('the_content', 'clear_replacedivopen');

Second we need also need to convert all closing div tags to closing paragraph tags.

1
2
3
4
5
// Replaces all </div> in the_content with </p>
function clear_replacedivclose($content){
	return str_replace("</div>","</p>", $content);
}
add_filter('the_content', 'clear_replacedivclose');

And finally clean up any empty paragraph tags left.

1
2
3
4
5
// Clears all empty <p></p> in the_content()
function clear_emptyp($content){
	return str_replace("<p></p>","", $content);
}
add_filter('the_content', 'clear_emptyp');

Now we have a formatted page!

A Summer’s Journey

“It has been a while…” Isn’t that how it always starts for any blogger that has not posted in over a month, in my case two? This summer has been a bit of mixed blessings. I met new people, enjoyed quite a few laughs, enjoyed my bank account a bit too much, and never found love. So let’s catch up!
Continue reading →

First Memorial Day Off in 5 Years

This past Monday’s Memorial Day was the first time in five years I had off from either work or class. I had the day planned out: wake up, get the daily Reach Achievements, wash some dirty laundry, clean, go out to eat with a friend or two and then relax for the remainder of the day. Great idea right?

Most of it went to plan. I even took out the trash. I was a busy little be until around 3pm. I haven’t eaten yet and I was getting hungry. I knew it was too late to each lunch but too early to eat dinner. I began sending out text messages to some friends on whether to eat or not.

Decline after decline, my heart sank lower and lower. Am I really going to spend my Memorial Day all alone? Fantastic!

Since I no longer have Facebook I was unknowing of a Brother’s birthday. Sucks, but it’s not like I was invited to celebrate it. I mean it’s not like he returns text messages anyways. Sigh, enough gripes. He’ll eventually come around and maybe text me back. Maybe.

Fast forward to this Tuesday evening when I shot after some food, and up came some bubbling dramatic crude. Yes, it was all me and I will take a bow. **Bows** Oh well. Lesson learned and so goes on to live another day. Am I still kinda pissed about being left out? Sure. Will I care later this week? Probably not. Will I call someone out on it? You betcha!

My first Memorial Day off in 5 years and I celebrated it with obligatory me time. Fail.