最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

posts - HTML Inside of Shortcode Breaks Shortcode Output

programmeradmin0浏览0评论

Lots of Googling has still not yielded a solution. Here is the problem:

I have created a custom Wordpress shortcode to use inside my Thesis theme. The goal of the shortcode is to allow users to wrap content on a page/post in Schema markup tags. My code is below:

function articlesection_rs_shortcode($atts, $content) {
    $content = do_shortcode($content);
    return '<span itemprop="articleSection">'.wpautop($content).'</span>';
}
add_shortcode('schema_article_section', 'articlesection_rs_shortcode');

But here is the issue. If the content between my shortcode opening/closing tags has no HTML elements, everything looks good. If, however, I have any HTML tags on inner content, the closing </span> gets placed at the very end of my content, instead of where I placed the closing shortcode tag. Visual example:

Output when content inside shortcode is plain-text:

<span itemprop="articleSection">test content</span>

Output when content inside shortcode is HTML content:

<span itemprop="articleSection">
    <h1>test content</h1>
    .........
    <!-- More Content Until end of page -->
</span>

I have tried using wpautop() and wptexturize() but no luck.

I am using Wordpress 3.8.2 and Thesis 1.8.5. Any help, advice, code, or links are much appreciated.

EDIT: Anyone have any ideas?

Lots of Googling has still not yielded a solution. Here is the problem:

I have created a custom Wordpress shortcode to use inside my Thesis theme. The goal of the shortcode is to allow users to wrap content on a page/post in Schema markup tags. My code is below:

function articlesection_rs_shortcode($atts, $content) {
    $content = do_shortcode($content);
    return '<span itemprop="articleSection">'.wpautop($content).'</span>';
}
add_shortcode('schema_article_section', 'articlesection_rs_shortcode');

But here is the issue. If the content between my shortcode opening/closing tags has no HTML elements, everything looks good. If, however, I have any HTML tags on inner content, the closing </span> gets placed at the very end of my content, instead of where I placed the closing shortcode tag. Visual example:

Output when content inside shortcode is plain-text:

<span itemprop="articleSection">test content</span>

Output when content inside shortcode is HTML content:

<span itemprop="articleSection">
    <h1>test content</h1>
    .........
    <!-- More Content Until end of page -->
</span>

I have tried using wpautop() and wptexturize() but no luck.

I am using Wordpress 3.8.2 and Thesis 1.8.5. Any help, advice, code, or links are much appreciated.

EDIT: Anyone have any ideas?

Share Improve this question edited Apr 14, 2014 at 19:38 Kirill M asked Apr 11, 2014 at 20:46 Kirill MKirill M 111 silver badge4 bronze badges 4
  • Example content that demonstrates the issue? Is <h1>test content</h1> enough? – Rarst Commented Apr 11, 2014 at 20:50
  • Its true for any HTML elements: h1, p, span, div, b, i, pre... you name it. I am only talking about basic HTML markup, the kind that a user could 'set' themselves by using the Wordpress WYSIWYG page/post editor. – Kirill M Commented Apr 11, 2014 at 22:04
  • Cannot reproduce. Your markup might be broken elsewhere and creating open/closing spans mismatch. – Rarst Commented Apr 14, 2014 at 20:47
  • Thanks Rarst. I will run some tests on another Wordpress install and see if I have theme issues or other issues. – Kirill M Commented Apr 14, 2014 at 21:10
Add a comment  | 

3 Answers 3

Reset to default 1

There was some weird issue with how Wordpress was handling line-breaks and auto styling between the shortcodes. After some more playing around was able to eliminate the issue. Very finicky system...

Here you go. I think the first problem was calling do_shortcode($content) that gets called by add_shortcode() so it's redundant here. Then I think calling wpautop($content) introduced another problem rather than getting closer to a solution.

function articlesection_rs_shortcode($atts, $content) {

return '<span itemprop="articleSection">'. $content .'</span>';
 }
 add_shortcode('schema_article_section', 'articlesection_rs_shortcode');

Try this:

function shortcode_func( $atts ) {
    ob_start();

    echo 'shortcode output';

    $output = ob_get_contents();
    ob_end_clean();
    echo $output; // Here comes the total output of your shortcode
}
add_shortcode( 'shortcode', 'shortcode_func' );
发布评论

评论列表(0)

  1. 暂无评论