Web Design and Build

WordPress wrapping shortcodes with p tags Last updated:15 June 2011

Been struggling over the last day or so to stop WordPress wrapping p tags around a shortcode. I’m using Arnan de Gans Events plugin on a WordPress site for a client. One of the pages just includes this shortcode:

[events_show]

This shows a list of events. I’ve configured the plugin so each event is in its own div. And it all works fine, except WordPress insists on wrapping the whole thing in a couple of p tags, making the code invalid, producing this:

<p><div class="event>.........</div></p>

Irritating. The WP shortcode API site says “wpautop recognizes shortcode syntax and will attempt not to wrap p or br tags around shortcodes that stand alone on a line by themselves”. Nooooo.

I finally solved this by adding the following to my functions.php file:

function simonbattersby_shortcode_format($content){
$content = preg_replace('/(<p>)\s*(<div)/','<div',$content);
$content =  preg_replace('/(<\/div>)\s*(<\/p>)/', '</div>', $content);
return $content;
}

add_filter('the_content','simonbattersby_shortcode_format',11);

Points to note – the filter needs to be set with priority 11 (or higher) because otherwise the shortcode won’t have produced its output, and it’ll have no effect. All the preg_replace lines are doing is looking for a <p> tag followed by <div>, with any number of whitespaces between them, and replacing with just a <div> tag. Brute force, inelegant, but it works, and I couldn’t find any other way….

Preventing doubleclick firing two clicks with jQuery Last updated:14 November 2010

A user of my wife’s workshop page reported that they couldn’t click to expand a div – when they did it opened and then immediately closed again. Experimentation showed that in fact they were double clicking rather than single clicking – registering two click events in fact. My original code was:

$('h3 span a').click(function() {
    $(this).parent().parent().siblings(".event_hidden").slideToggle();
    return false;
});

When I started thinking about it, I couldn’t immediately see how to separate the two events – after all, a doubleclick is just two clicks, isn’t it? Fortunately jQuery provides an easy solution by using the event properties:

$('h3 span a').click(function(event) {
    if(event.detail==1){//activate on first click only to avoid hiding again on double clicks
        $(this).parent().parent().siblings(".event_hidden").slideToggle();
    }
    return false;
});

Edit 14 November 2010 Actually, problem not quite solved, since I found that IE8 and under (I don’t know about IE9) don’t recognise event.detail, and hence the code above prevents the slideToggle working entirely. Fortunately, neither does the doubleclick problem seem to affect these browsers, and hence I amended my code to:

$('h3 span a').click(function(event) {
    if(!event.detail || event.detail==1){//activate on first click only to avoid hiding again on double clicks
        $(this).parent().parent().siblings(".event_hidden").slideToggle();
    }
    return false;
});

Body classes on WordPress pages Last updated:11 November 2010

I spent hours this weekend trying to apply a different body class to a WordPress page. Specifically, I wanted to apply a different class to WordPress pages as opposed to WordPress posts. Why? Because I wanted to be allow comments to be left on a number of pages which were previously outside WordPress, but I wanted the menu, in line with the rest of my site, to display the submenu for either web pages or photo pages, depending on the actual page displayed. My css controls this by using the body class.

Once I’d worked it out it was straightforward, inevitably. First off, I used the “Custom Fields” functionality to define a custom field called bodyclass, then set this to “web” or “photo” as my css requires. Easy so far.

I then amended header.php using WordPress conditional comments to use the bodyclass value for pages, and to use “blog” as standard for posts. The following statement did the trick here:

<body class="
<?php if (is_page()) echo get_post_meta($post->ID, 'bodyclass', true); 
else echo "blog";?>
">

jQuery fadeIn and fadeOut problems with IE8 Last updated:29 October 2010

I’ve just written a jQuery plugin to allow a user to simply configure some dropdown menu animations. It tests fine in IE6 and IE7, but the fadeIn/fadeOut didn’t work in IE8. Unbelievable. It astonishes me the things that fail in IE8 but were fine in IE7.

The specific instance here was fading a drop down ul element, which was positioned absolutely with respect to an li element with position:relative. You know, standard stuff. Turns out IE8, position:relative and fade don’t play nicely. The solution in the end turned out to be applying:

filter:inherit

to the li element. Applying this to the ul has no effect. This doesn’t seem to cause any issues with IE6 or IE7, fortunately.

I also found that in IE8 as the fadeIn completes, the faded text appears to jump a pixel to the left. This isn’t tragic, but it’s a bit irritating. I found that this only happened if I used the fadeIn method. If I fade in by using .animate() to change the opacity, then the jump goes away.

You can see the final result here if you’re interested.

Experiments with @font-face Last updated:29 October 2010

I’ve previously been a fan of Cufon for rendering web fonts, but this morning I thought I’d have a play around with @font-face. I tried it, of course, in Firefox first, and it worked. Hurrah. But IE uses its own proprietary font format .eot, created, or so I thought, by Microsoft’s own product WEFT. I’d heard the WEFT tool was unpleasant to use, but this still hadn’t prepared me for the grim reality….

However, via this excellent article I found Font Squirrel which does all the work for you.

Internet Explorer continues to be a pain, however, and applying the necessary css for IE needs some novel css construction – see the article for details. Having overcome this…

…you should see this text in a NASA-like font…

…unless you’re viewing in Opera 9, which doesn’t support @font-face. Otherwise, nice.

EDIT THE NEXT MORNING: After further experimentation I’m slightly put off by the “flash of unstyled text” which is noticeable in Firefox at least. There are some javascript hacks to try and address this, but if I’ve got to use javascript I think I’ll stick to Cufon for now.