Reputation: 1660
I'm trying to create some short codes for my wordpress theme but i'm getting an issue with wordpress adding closing p tags.
Here's my short code that's in the functions.php file
function fixed_background( $atts, $content = null ) {
extract(shortcode_atts(array(
"class" => '',
"img_src" => '#',
), $atts));
$return =
'<div class="fixed-background-image-container">
<div class="fixed-background-image"><span style="background-image: url('.$img_src.')" class="background-fixed"></span>
<div class="block content-960 center-relative">' . do_shortcode($content) . '</div>
</div>
</div>
<div class="clear"></div>';
return $return;
}
add_shortcode("fixed_background", "fixed_background");
Here is a result of the output in html:
<div class="fixed-background-image-container">
<div class="fixed-background-image"><span style="background-image: url(http://www.blahblahblah.co.uk/photo2.jpg)" class="background-fixed"></span> </p>
<div class="block content-960 center-relative">
</div></div>
</p></div>
<div class="clear"></div>
Notice that there is 2 p tags placed inside, how can i get rid of these?
I'm also disabling wordpress's auto-formatting feature with the code below:
function webtreats_formatter($content) {
$new_content = '';
/* Matches the contents and the open and closing tags */
$pattern_full = '{(\[raw\].*?\[/raw\])}is';
/* Matches just the contents */
$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
/* Divide content into pieces */
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
/* Loop over pieces */
foreach ($pieces as $piece) {
/* Look for presence of the shortcode */
if (preg_match($pattern_contents, $piece, $matches)) {
/* Append to content (no formatting) */
$new_content .= $matches[1];
} else {
/* Format and append to content */
$new_content .= wptexturize(wpautop($piece));
}
}
return $new_content;
}
// Remove the 2 main auto-formatters
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
// Before displaying for viewing, apply this function
add_filter('the_content', 'webtreats_formatter', 99);
add_filter('widget_text', 'webtreats_formatter', 99);
P.S On a side note, is there anyway I can tidy up the coding, see example:
<div class="blahblah">
<div class="dv2">
<div class="dv3">
</div></div></div>
but have it more like
<div class="blahblah">
<div class="dv2">
<div class="dv3">
</div>
</div>
</div>
Upvotes: 0
Views: 1772
Reputation: 3316
You can try using the following code to fix the empty paragraphs:
add_filter('the_content', 'fix_shortcode_empty_paragraphs');
function fix_shortcode_empty_paragraphs($content) {
$array = array(
'<p>[' => '[',
']</p>' => ']',
']<br />' => ']',
);
$content = strtr($content, $array);
return $content;
}
And don't forget to remove the current fixes that you're applying to disable the auto formatting.
Upvotes: 2