Thoughtpick: A unique perspective of the Web and social media...
A unique perspective of the Web and social media...

October 31, 2009

HOW TO: Build a XHTML Valid Wordpress Blog with DISQUS Plugin

The last couple of days, I was reading about how to get your site to rank higher in Google. There are many tweaks you can do to insure that your site is Google friendly. One of the controversial topics was whether having your pages XHTML/HTML valid helps in SEO ranking or not. It’s clear that if your site has made a mess out of the HTML code, a search engine spider might have a tough time reading and parsing your page, which might lead to errors and in turn lower ranking. So, I decided that it’s better to be safe than sorry, and started converting the entire blog to XHTML 1.0 Transitional valid.

Thoughtpick Valid XHTML

Thoughtpick Valid XHTML

Let’s start…

The main issues targeted by this post

On the Thoughtpick blog, we have a few plugins that were causing XHTML validation issues. Mainly:

  • DISQUS Commenting System: At least 4 or 5 XHTML validation errors were generated by this plugin. The solutions are listed below.
  • Wordpress 2.8+ “role” problem, and how to fix it.
  • Youtube and Vimeo embed object issues: the <embed> tag is invalid for XHTML 1.0, to fix the issue, I wrote a small function.

Tools recommended

A few tools are recommended to make fixing your particular blog validation issues easier. Mainly:

  1. The official W3C Markup Validation Service: http://validator.w3.org/
  2. Total Validator – this tool provides an easier to read way of locating HTML syntax errors and made this task much easier: http://www.totalvalidator.com/
  3. Wordpress admin section for editing the theme. You need to “chmod 666” your them files to be able to edit from within WP-Admin. Or a text editor and your favorite FTP client. Your call!

The fixes…

1. Fixing the broken XHTML of DISQUS

DISQUS Commenting System (version: 2.12.7121) seems to result in a bunch of errors. Keep in mind that the line numbers are just given to help you locate the line easier, but if you have the slightest difference in version, you need to look for it. Here they are and how to fix them:

1a) DISQUS has an extra “</a>”

DISQUS has an extra “</a>” on line 322 of the “disqus-comment-system/disqus.php” file. Just change the “… . $post->ID . ‘”>Comments</a>” to “… . $post->ID . ‘”>Comments“:

DISQUS missing tag and problematic id

DISQUS missing tag and problematic id

1b) DISQUS uses invalid “id” tags

The same “disqus-comment-system/disqus.php” file, line 322 as well, change the “… ‘#disqus_thread” id=”‘ …” to “… ‘#disqus_thread” id=”dsq …”. Just add ‘dsq‘ at the beginning of the ids to avoid the “value of attribute “id” invalid: “1″ cannot start a name” XHTML validation errors.

That’s not all, you also need to change the Javascript at line 297 to use accommodate for the ids starting with “dsq” instead of being just numerical. See the image for the changes:

DISQUS Replace 'dsq' in IDs

DISQUS Replace 'dsq' in IDs

1c) Fixing the “&” and the “<br>”

The <br> (without the closing /) is not valid XHTML, and the & characters (instead of &amp;) is also invalid. In the “disqus-comment-system/comments.php” file, on line 21, replace the “… echo $comment['message']); …” by the following str_ireplace() function:

DISQUS fixing the br and amps

DISQUS fixing the br and amps

1d) DISQUS outputs empty unordered lists (ul)

The last DISQUS issue is that if there are no comments, it displays an empty unordered list <ul id=”dsq-comments”></ul>. To fix that, we simply need to check if there are comments or not before we output that. You need to do the following changes to the <ul id=”dsq-comments”> and </ul> parts of the code on lines 7 and 25:

DISQUS fix empty unorder list error

DISQUS fix empty unordered list error

To get the the fixed code for DISQUS Wordpress plugin version 2.12.7121, they are uploaded on pastebin: download comments.php file, download disqus.php file.

2. For Wordpress 2.8+, remove role=”search”

Wordpress 2.8+ uses the HTML5 tag “role”. To validate successfully as XHTML 1.0, you need to remove that. Just remove “role=”search”” from the wp-includes/general-template.php file. You can download the edited version of general-template.php here for Wordpress v2.8.4.

Remove role=search from general-template.php

Remove role=search from general-template.php

3. Fix your header.php, index.php and footer.php to be XHTML valid

This part is custom for each blog. Mainly you need to make sure that each <tag> is closed with relevant </tag>, or using the alternative <tag /> annotation. Another point to keep in mind is that XHTML “id” attributes must start with an alphabetical letter (not a number), and all tags must be lower case (<META … /> or <BR /> will result in errors). Finally, all images must have the “alt” attribute defined.

4. Youtube embeds are invalid XHTML

Youtube embed code has an invalid XHTML tag “embed”. I wrote a function that does DOM traversal to fix this issue, without bothering your blog contributors to use a custom made function. I will discuss the code in another post, so subscribe to our feed and keep an eye out for the post. Meanwhile, the Fix Video Objects function code can be found here.

If you face any problems or have any questions, feel free to ask…

  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • StumbleUpon
  • Sphinn
  • Slashdot
  • Google Bookmarks
  • Mixx
  • Ping.fm
  • FriendFeed
  • BlinkList
  • Technorati
  • HackerNews
  • email

Possibly related posts:

  1. HOW TO: 14 Tips to Promote Your Remarkable Wordpress Plugin
  2. We’ve just moved. Come have a look!
  3. App Review: KwiClick’s Firefox Search Plugin
  4. 50 Top Noise-free Twitter Tips & Links in Marketing, SEO, Design & Writing!
  5. HOW TO: Blog About the Latest News Scoops Using Yahoo! Pipes (Part 1)
Amer Kawar: Founder of ThoughtPick, a 2009 start-up "aiming to make the Internet a smoother surf". Based on a profound interest in information and technology, Amer was able to nourish this interest through studying at the University of Liverpool and graduating with a Bachelor's degree in Electrical Engineering followed by a Masters degree in Computer Network Security.
  • Hi Amer, thanks for this great post.

    Your updated versions of comments.php and disqus.php solved the XHTML validation errors I was getting. If you're reading this Disqus please can you fix them in the default code so we don't need to hack the code :)

    Amer - are you able to work your magic on the CSS that Disqus pumps out?

    I get W3C CSS validation errors no matter which built-in Disqus stylesheet I choose:
    - Narcissus (113 errors)
    - A (71 errors)
    - B (88 errors)

    There is an option to add your own Custom CSS code, but this doesn't seem to remove the built-in CSS rules Disqus applies. Any suggestions?

    I think you are using the Disqus Narcissus theme as you have 113 CSS errors from the Disqus plugin:
    http://jigsaw.w3.org/css-validator/validator?ur...
  • Hi, Thanks for commenting.

    I am aware of that, but CSS is too much of a hassle with the need to test on all those browsers from IE6 to the versions of Chrome until most bugs were ironed out (v4).

    If you are in the mood to take on this challenge, the DISQUS function file+function that embeds the "disqus.css?v=2.0" file is "disqus.php" (in my version line 222):

    function dsq_get_style() {
    echo <link rel=\"stylesheet\" href=\"" . DISQUS_API_URL ."/stylesheets/" . strtolower(get_option('disqus_forum_url')) . "/disqus.css?v=2.0\" type=\"text/css\" media=\"screen\" />;
    }

    Here, you can download the CSS file locally, and override this function (edit it directly) to link to the local file which you fix the CSS errors in.

    For me to put my head into CSS is too much of a hassle that I'd prefer to go back to WP's default comments. :)

    Hope this is helpful.
  • cheers Amer

    I think that disquss.css?v2.0 file contains just your own customizations, rather than the full Narcissus css file

    eg. mine has only 2 rules and reads:

    #dsq-content iframe, #dsq-popup-alert iframe {
    height: 355px;
    }

    /*
    * Narcissus theme customization
    */
    #dsq-content .dsq-header-avatar img {
    width: 32px;
    height: 32px;
    }


    Do you know the URL where I can download the original Narcissus css file in full (so I can look at how to over-ride parts of it?)

    Any idea how to over-ride for example "-webkit-border-bottom-right-radius" so that this property isn't called?

    ------

    82 #dsq-content #dsq-comments .dsq-header-avatar .dsq-drop-profile Property -webkit-border-bottom-right-radius doesn't exist : 5px 5px
    83
    #dsq-content #dsq-comments .dsq-header-avatar .dsq-drop-profile Property -webkit-border-bottom-left-radius doesn't exist : 5px 5px

    88 #dsq-content #dsq-comments .dsq-header-avatar .dsq-drop-profile Property _position doesn't exist : absolute absolute

    108 .dsq-overlay Property -moz-opacity doesn't exist : 0.8 0.8
  • cool, I didn't spot the option to select CSS3 under More Options on the W3C CSS validator
    Still churns out that most of the code is invalid, so I'm gonna look into this:
    http://jigsaw.w3.org/css-validator/validator?ur...

    The blog I was talking about that I'm currently trying to fix errors on is
    http://www.adamandjoe.com
    http://www.adamandjoe.com/2010/01/27/the-lafta-...
    so I think the CSS stylesheet is:
    http://disqus.com/stylesheets/adamandjoe/disqus...


    http://disqus.com/stylesheets/hobbsy/disqus.css...
    ^ I think is for my main blog (linked from my Disqus profile) http://www.hobbsy.com
  • Well, I think your URL is: http://disqus.com/stylesheets/hobbsy/disqus.css...

    As it turns out, CSS does not depend on versioning. So, maybe you just need to click on the "More options" button on the CSS Validator and pick v3 by yourself.

    I didn't tackle this issue on full scale yet, so you're going to need to do some researching to learn what's valid and what's not. Keep in mind, not all browsers support all the features of CSS3. For example IE8 does not support CSS3's rounded corners.
  • Thanks again Amer

    My Disqus CSS is:
    http://disqus.com/stylesheets/adamandjoe/disqus...

    As you can see, there only seems to be 2 rules in there

    I will Google around and see if I can find out how to make my stylesheet switch into CSS3 mode rather than 2.1 (hopefully it's the same sort of method as stipulating Transitional in the XHTML first line)
  • Hi again,

    Well, the CSS file you want is: http://disqus.com/stylesheets/thoughtpickblog/d... (change the URL based on your blog's name). If you download that, store it on your server and edit the "disqus.php" function to point locally rather than to disqus.com, that should be your starting point without depending on DISQUS's hosted CSS.

    Regarding your example "-webkit-border-bottom-right-radius", this is a CSS3 tag while your CSS is defined as CSS 2.1. You need to look into that.

    Good luck :)
  • cyberhobo
    I haven't tested fixVideoObjects thoroughly yet, but it seems to work. If so, you deserve at least a small medal for it - thanks!
  • Thanks for that :). I appreciate it.
  • I've been putting off fixing the Disqus errors because I knew it would take me FOREVER to sift through all of that php. Your solutions worked like a charm. Thanks so much for this post!
  • Thank you very much. The Disqus XHTML errors were driving me crazy, this fixed it without any effort.
  • lol~it is very useful for me ,thanks ,thanks
  • Wow that was great post, i'm gonna try in my blog soon. Thanks
  • disqus is very easy to use
  • Great post! Those should be built-in inside the next Disqus release hopefully.
  • I am getting this really weird error when i use your method:
    http://drp.ly/8H34D
    And i still get some errors in w3c validator total validator:
    http://drp.ly/8H4dw
    I hope you can take a look. Thanks mate! :)
  • Hi there, well first make sure you are using "XHTML 1.0 Transitional". I think it's either that or you have an unclosed tag before the "noscript" tag, as the noscript is an entirely valid XHTML tag, and this blog is an example.

    Also, try using http://www.totalvalidator.com/index.html, it usually previews easier to read errors.

    Let me know if you need more help by replying or using the contact us form on the top.

    Happy New Year! :)
  • Thank you for the answer! But the problem is that this error with multiple disqus comment counts only appears when i use your "fix". I am using XHTML 1.0 Transitional.

    My site:
    http://guideheaven.com
  • Just to let you know, Disqus causes massive CSS errors. It personally causes over 100 on my blog.
  • Thanks for giving the procedure of building a xhtml wordpress blog with Disqus plug in. I was looking for this since a long time.
  • audrey159
    DISQUS Commenting System seems to result in a bunch of errors. What is its solution?
  • The tips mentioned above did not work for you?
  • Thanks man, just what I was looking for. Worked like a charm Thanks so much…
  • sis
    another plugin, that handles comments too:
    http://wordpress.org/extend/plugins/smart-youtube/
    you only need to use "httpv" instead of "http" in youtube links
  • sis
    hi,
    I started to implement your code fix video object,
    then realized there must be a plugin for this issue.
    There is http://wordpress.org/extend/plugins/wp-youtube/
    so i stopped implementing. With this plugin you only need to specify
    tagnames like [wpyt_myvideo] then put eg. [wpyt_myvideo]U6USH61YxAY[/wpyt_myvideo]
    into your posts. Afaik it does not handle comments at this time.
    Btw. your blog is great, thanks for that,
    the only reason i wrote this comment to improve it.
    sis
  • Rockstarsid
    Awesome article.. was searching for ways to fix my errors and disqus surely has bugs..

    But one thing, I wasn't able to fix them. Some of the codes you mentioned (1b, 1c) aren't seen on my Disqus.php .. maybe coz I upgraded it recently to the newer version.

    20 errors remains! Sigh.. but a great job mate :)
  • Hi mate. Try http://totalvalidator.com/. I just ran it on your site, and you seem to have many upper case tag definitions, duplicated "id" attributes, and a missing tag from the header section:
    <meta http-equiv="Content-Style-Type" content="text/css" />

    Try to fix these and let me know if it works out for you. :)
  • hi friend i agree with you for i help from here and many key word help to reach top rank.
  • Rockstarsid
    I am sorry but I don't know how to fix them and where they are located.. actually I have 2 css files. One is style.css and the other one is custom.css and is there any tool to fix these stuffs automatically?

    Thanks.
  • I'm not aware of any tools that fix XHTML and CSS errors automatically. Why don't you use the contact form on the blog to contact me with your email address, I can try to help you out. :)
blog comments powered by Disqus