I’m probably doing this all wrong, and there’ll be a much simpler solution. Basically, I’m trying to build a table of tour dates that’ll be editable by someone with little technical knowledge, but can handle shortcodes. There’s also some custom functions in the table (with regards to displaying the date, and having an expiring ticket link) that makes me want to avoid a clunky table plugin.
So here’s the code I’m experimenting with, that’s working apart from it’s introducing a
tag for each new line of and separate short code.
I don’t want to disable wpautop completely, as it’s used widely throughout the website and elsewhere on the page that this table will be appearing on.
[tour_table tour_name="The Tour Name"]
[tour_table_line tour_date="2011-06-19" ticket_link="http://ticket.link/" venue="The Venue Name"]
[tour_table_line tour_date="2011-06-19" ticket_link="http://ticket.link/" venue="The Venue Name"]
[tour_table_line tour_date="2011-06-19" ticket_link="http://ticket.link/" venue="The Venue Name"]
[tour_table_line tour_date="2011-06-19" ticket_link="http://ticket.link/" venue="The Venue Name"]
[tour_table_line tour_date="2011-06-19" ticket_link="http://ticket.link/" venue="The Venue Name"]
[tour_table_line tour_date="2011-06-19" ticket_link="http://ticket.link/" venue="The Venue Name"]
[/table]
The parsing of the table is all handled in functions.php by the wrapping function tour_table() and the function [tour_table_line] which is executed inside a do_shortcode() function (from the wrapping function).
So the basic problem is that each new line is earning itself a new
tag, which is placing a massive space above the table.
If anyone has any more elegant solutions, or can help me get the functionality I’m looking for, I’d be very appreciative of your help.
If you need more detail, please let me know.
Edit to add functions (probably quite a bit of convoluted code in there:
function tour_table($atts,$content = null){
extract( shortcode_atts( array(
'tour_name' => '',
'anchor_table' => ''
), $atts ) );
if($anchor_table!=""){
$hid = "id="tourscrollto"";
}else{
$hid = "";
}
$theoutput = "<h3 $hid style="text-align: left; font-weight: strong; font-size: 12pt; margin-top: 20px;">";
$theoutput .= $tour_name;
$theoutput .= "</h3><table class="tourdates" style="font-size: 10pt;" width="100%"><tbody>";
$theoutput .= do_shortcode($content);
$theoutput .= "</tbody></table>";
return $theoutput;
}
function tour_table_line($atts){
extract( shortcode_atts( array(
'tour_date' => '',
'ticket_link' => '',
'venue' => 'The Venue'
), $atts ) );
$date_original = $tour_date;
$date_unix = strtotime($tour_date);
$theoutput = "<tr><td>";
$theoutput .= date('D',$date_unix);
$theoutput .= "</td><td>";
$theoutput .= date('j M',$date_unix);
$theoutput .= "</td><td>";
$theoutput .= $venue;
$theoutput .= "</td><td>";
//$ticketlink = ticketlinkexpire($date_original,"<a href="$ticket_link" target="_blank">Buy Tickets</a>","");
//$theoutput .= $ticketlink;
$theoutput .= "</td></tr>";
return $theoutput;
}
add_shortcode( 'tour_table' , 'tour_table' );
add_shortcode( 'tour_table_line' , 'tour_table_line' );
The extra space comes from
wpautop()
, which inserts<br />
on every line break. You have to strip these out before callingdo_shortcode()
.Additionally, use
add_filter( 'the_content', 'shortcode_unautop' );
. From my experience, you need both. Probably a bug.See my shortcode plugin for an example. It has shortcodes for tables too.
Aside: Shortcodes should never be part of a theme, because you lock yourself or your client into using that theme now. See Justin Tadlockâs article.
I use this in functions.php and then bracket the code in the editor I don’t want autop’d with
<!-- noformat on -->
and <!-- noformat off -->
lines.Have you heard of gigpress ? This would give you the functionality you want in a nice wordpress like way (user friendly input). It would mean you’d have to throw some code away and modify your theme, but would save you a lot of time in the long run.