An improvement, and another use for the safetags function

A while ago I wrote a function which checks HTML snippets and ensures that tags which should be closed are removed unless they are actually closed. For example, if user types

<div style="whatever">Blah Blah

and forgets to close the div tag, then that div tag will be removed. Sure it would be better if the div tag was fixed, but that requires more code. This is good enough for me.

Another use for this function is excerpts. An original article may have all the tags properly closed but the excerpt might have an opening tag with it’s matching tag cut off.

A copy of the function is here.

It is not a validator. It ignores tags like p and br which will work even if not coded properly. You can edit the function to change which tags are allowed.

I used the function to alter Virginia CURE’s website from the original cover-wp theme. For comparison, I found a couple of other websites which use cover-wp: NELA Lives, Hedge Futures, and Marco Luthe. I’m not familiar with these websites, I just looked for websites that use the cover-wp theme. Eventually, any of these websites might disappear, or any of them might change themes, including Virginia CURE. But as I’m writing this they’re good examples. Virginia CURE’s excepts have the paragraphs preserved, the others don’t. The cover-wp theme uses a customized get_the_excerpt function, which in turn calls the built-in php function striptags to remove all of the tags.

Here’s copy of functions.php which I altered from cover-wp. I renamed it functions.txt so it can be viewed. I also made a small change to get_the_excerpt.

If anyone who reads this knows of a better function then I’d be interested. I’m just an amateur hack. But for now, my function works better than anything I found.

I fixed a couple of problems since I first posted it.

Advertisements
Previous Post
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s