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?

  • Example content that demonstrates the issue? Is <h1>test content</h1> enough? – Rarst Apr 11 '14 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 Apr 11 '14 at 22:04
  • Cannot reproduce. Your markup might be broken elsewhere and creating open/closing spans mismatch. – Rarst Apr 14 '14 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 Apr 14 '14 at 21:10
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...

0

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

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.