Remember a couple of years ago when everyone was crazy about validating their HTML and CSS? I’m glad the validation nazis are no longer in power because, practically speaking, you can’t always have perfectly valid HTML.
That said, I still think checking your HTML on a the W3C validator is valuable sometimes. It could help you find out about unclosed HTML tags or other serious errors in your markup.
A good time to validate your HTML is when you publish a new post or when you update it–especially if you use shortcodes that generate HTML or you yourself use HTML in WordPress HTML editor…
Wouldn’t it be cool if WordPress showed you a message like this whenever you published a post:

Or when you updated the post:

Actually, with the help of the little code snippet below, which you can add to your functions.php file, you can have just that.
add_filter('post_updated_messages', 'wink_post_updated_message'); function wink_post_updated_message($messages) { $or_validate_its_html = ' or <a href="http://validator.w3.org/check?uri=' . urlencode(get_permalink($_GET['post'])) . '" target="_blank">validate its HTML</a>'; $messages['post'][1] .= $or_validate_its_html; $messages['page'][1] .= $or_validate_its_html; $messages['post'][6] .= $or_validate_its_html; $messages['page'][6] .= $or_validate_its_html; return $messages; } |
With this code, when you click on the ‘validate its HTML’ link, the W3C validator will open in a new window and validate your newly published or updated URL. Very simple and effective, I think.
Thanks, nice post and helpful.
Any thoughts if my scenario is as follows?
I want to add a custom validation for a Post before it is published. I am able to perform my validation logic. If we find an error (as per the validation), how do I set an error message and display it? I’m calling the WP_Error message as per posts on WP site but it seems to be sending header error message and site breaks.
Thanks
I have no idea.