Tag: code

  • Lightbox 2 and XHTML 1.1 validation

    I recently started using the Lightbox 2 WordPress plugin on my Marbella news site to automatically add the zooming overlay effect to certain images. It is really plug & play and works out of the box.

    I did encounter however that my pages stopped validating as XHTML 1.1 which is the standard doctype on all sites I develop.

    The error in question is due to the square brackets (“[” and “]”) being used to link individual images in one single post together as a gallery under the “rel” attribute. The square brackets are not permitted characters and therefore the W3 Validator spits out the following error message:

    character "[" is not allowed in the value of attribute "rel"
    It is possible that you violated the naming convention for this attribute. For example, id and name attributes must begin with a letter, not a digit.

    To avoid this XHTML 1.1 validation problem simply edit the following line in the Lightbox 2 plugin file named lightbox2.php under the “autoexpand_rel_wlightbox” function:

    from:

    $replacement    = '$1 rel="lightbox['.$post->ID.']">';

    to:

    $replacement    = '$1 rel="lightbox_'.$post->ID.'">';
    

    You should now be able to validate that plugin’s output as XHTML 1.1 with no issues.

  • WordPress + CDATA nightmare

    Do you know what is worse than not knowing the solution or even the reason to a problem?

    Well, that must be, knowing that you have experienced the same problem in the past and resolved it, but no matter how much you try, you can not remember at all what the problem was due and what you did to fix it.

    So this evening several hours went by due to a silly and stupid WordPress issue I already fixed once in the past but lost during an upgrade. This time I wanted to make sure I got the solution written down as I would probably forget it again.

    On my Marbella news site I was working on embedding an external flash video inside an iframe, which itself was invoked via Javascript to avoid the W3 Validator complaining about XHTML 1.1 and iframes being taboo.

    Basically it was WordPress’ over-zealous filters that caught a greater-than sign (>) and converted it to the equivalent HTML entity (>). The most annoying thing is that I already have the Raw HTML plugin installed which disables WordPress’ text filtering. And it also worked fine for the rest of the Javascript and HTML tags in the page text. It was just on one single character, namely in the CDATA closing tag with // ]]> converted to // ]]>.

    After searching many different combinations I finally hit it on the nail. I was not alone either.

    The annoying culprit is this little line in /wp-includes/post-template.php, which I once more commented out:

    $content = str_replace(']]>', ']]>', $content);

    For some reason the WordPress developers have not yet fixed this issue after 3 years of it being reported.

    As a big fan of open source I appreciate the time and money spent by volunteers in many similar projects, however this is not the first time I see quick to fix issues hanging around for ages, simply due to people preferring to have an academic discussion on who can provide the most elegant solution, instead of simply fixing the issue first and talk later.

    Sometimes I wonder how projects ever become real projects.

    Anyhow there was a happy ending as you can see for yourself.

  • Upload & Insert images as a WordPress contributor

    For my Marbella news site I recently hired an excellent free-lance writer on Elance to assist me in composing quality articles. As the site is also powered by WordPress I added him as a contributor to start of with. He explained he had some trouble adding images per the guide I sent him. The “Upload/Insert” image button was simply not there.

    So to summarise, the WordPress contributor role apparently lacks this permission. There are a few plugins around that can let you tweak those permissions with more granularity, but I just needed that single change.

    A bit of searching revealed exactly what I needed. One minor change in the theme’s functions file was sufficient to upload & insert images as a WordPress contributor:

    if ( current_user_can('contributor') && !current_user_can('upload_files') )
    	add_action('admin_init', 'allow_contributor_uploads');
     
    function allow_contributor_uploads() {
    	$contributor = get_role('contributor');
    	$contributor->add_cap('upload_files');
    }
    

    This also keeps the core clean for changes which makes upgrading WordPress later on less time consuming.