admin管理员组文章数量:1122846
I'm developing a custom shortcode in WordPress to display a calendar, but I'm encountering an issue where the HTML output is being unexpectedly wrapped in
tags. Here's the code I'm using for the shortcode:
function wp_reservations_shortcode($atts) {
ob_start();
?>
<section>
<section class="calendar-header">
<button id="prev-month">Prev</button>
<div class="current-date">July 2024</div>
<button id="next-month">Next</button>
</section>
<section class="calendar-body">
<div class="calendar-weekdays"></div>
<div class="calendar-days"></div>
</section>
</section>
<?php
$html = ob_get_clean();
return $html;
}
add_shortcode('wp_reservations', 'wp_reservations_shortcode');
When I use this shortcode, the HTML output includes unwanted p and br tags like this:
<section>
<section class="calendar-header">
<button id="prev-month">Prev</button><p></p>
<div class="current-date">July 2024</div>
<p><button id="next-month">Next</button><br></p>
</section>
<section class="calendar-body">
<div class="calendar-weekdays"></div>
<div class="calendar-days"></div>
</section>
</section>
I've tried removing the wpautop filter with remove_filter('the_content', 'wpautop'); before returning the HTML, but it didn’t resolve the issue. I attempted to directly echo the HTML output instead of returning it from the shortcode, which also didn't help. Lastly I tried storing the HTML in a variable and then returning it also failed to remove the p tags.
How can I prevent WordPress from inserting these tags in the output of my shortcode?
Thank you for your help!
Edit: After further investigation, I've discovered that the issue with unwanted p tags occurs only when the shortcode is used within the site template. When the shortcode is used outside of the template, it functions as expected. Interestingly, other similar plugins do not exhibit this behavior when used in the same context.
I'm developing a custom shortcode in WordPress to display a calendar, but I'm encountering an issue where the HTML output is being unexpectedly wrapped in
tags. Here's the code I'm using for the shortcode:
function wp_reservations_shortcode($atts) {
ob_start();
?>
<section>
<section class="calendar-header">
<button id="prev-month">Prev</button>
<div class="current-date">July 2024</div>
<button id="next-month">Next</button>
</section>
<section class="calendar-body">
<div class="calendar-weekdays"></div>
<div class="calendar-days"></div>
</section>
</section>
<?php
$html = ob_get_clean();
return $html;
}
add_shortcode('wp_reservations', 'wp_reservations_shortcode');
When I use this shortcode, the HTML output includes unwanted p and br tags like this:
<section>
<section class="calendar-header">
<button id="prev-month">Prev</button><p></p>
<div class="current-date">July 2024</div>
<p><button id="next-month">Next</button><br></p>
</section>
<section class="calendar-body">
<div class="calendar-weekdays"></div>
<div class="calendar-days"></div>
</section>
</section>
I've tried removing the wpautop filter with remove_filter('the_content', 'wpautop'); before returning the HTML, but it didn’t resolve the issue. I attempted to directly echo the HTML output instead of returning it from the shortcode, which also didn't help. Lastly I tried storing the HTML in a variable and then returning it also failed to remove the p tags.
How can I prevent WordPress from inserting these tags in the output of my shortcode?
Thank you for your help!
Edit: After further investigation, I've discovered that the issue with unwanted p tags occurs only when the shortcode is used within the site template. When the shortcode is used outside of the template, it functions as expected. Interestingly, other similar plugins do not exhibit this behavior when used in the same context.
Share Improve this question edited Apr 20, 2024 at 8:59 Alex asked Apr 19, 2024 at 19:49 AlexAlex 112 bronze badges 1 |1 Answer
Reset to default 0Please try as below:
function wp_reservations_shortcode($atts) {
// remove auto paragraphs for shortcode content
remove_filter('the_content', 'wpautop');
remove_filter('the_excerpt', 'wpautop');
ob_start();
?>
<section>
<section class="calendar-header">
<button id="prev-month">Prev</button>
<div class="current-date">July 2024</div>
<button id="next-month">Next</button>
</section>
<section class="calendar-body">
<div class="calendar-weekdays"></div>
<div class="calendar-days"></div>
</section>
</section>
<?php
$html = ob_get_clean();
// Re-enable automatic paragraphs
add_filter('the_content', 'wpautop');
add_filter('the_excerpt', 'wpautop');
return $html;
}
add_shortcode('wp_reservations', 'wp_reservations_shortcode');
本文标签: pluginsHTML output from WordPress shortcode is wrapped in unexpected ltpgt tags
版权声明:本文标题:plugins - HTML output from WordPress shortcode is wrapped in unexpected <p> tags 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736309862a1934174.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<section class="calendar-header"><button id="prev-month">Prev</button><p></p><div class="current-date">July 2024</div><p><button id="next-month">Next</button><br></p></section>
. Also, FWIW, embedding <sections> in <sections> isn't semantically correct. – Tony Djukic Commented Apr 22, 2024 at 21:56