• Hi everyone,

    I have a plugin that generates an accordion (using Zurb Foundation framework) using shortcodes. The essential output should be:

    <ul class="accordion">
        <li class="accordion-item">
           <a href="#" class="accordion-title"><h3>Accordion item title</h3></a>
           <div class="accordion-content">The content</div>
        </li>
    </ul>
    

    The code to output the title is the issue. The relevant part is:

    $accordion_title = sprintf('<a id="%3$s" class="accordion-title" role="tab" tabindex="0"><h3>%2$s</h3></a>',
    

    This works perfectly fine if I use a <span> not a <h3>, but for semantic and SEO reasons I need the title to be a heading.

    In functions.php I have the following code to clean up the <p> tags from shortcodes:

    // Remove wpautop from wherever it is
    remove_filter('the_content', 'wpautop');
    remove_filter('term_description', 'wpautop');
    // Add it pack at priority 99
    add_filter('the_content', 'wpautop', 99);
    add_filter('term_description', 'wpautop', 99);
    // Run shortcode_unautop after wpautop
    add_filter('the_content', 'shortcode_unautop', 100);
    add_filter('term_description', 'shortcode_unautop', 100);
    

    This works great everywhere except the accordion title. If I keep wpautop on, the <a> tag has an extra <p> tag output after it with the link in it again but empty (so essentially the link is there twice but only one has text in it and works). It works fine with wpautop off, but I need wpautop everywhere else so turning it off isn’t a solution.

    I also tried putting the <h3> before the <a>, not inside it, but then the <a> just gets wrapped in a <p> which stops the accordion from working.

    How can I stop these extra <p> tags from being generated?

    Thank you so much for your time!

    • This topic was modified 8 years, 12 months ago by doubleedesign.
    • This topic was modified 8 years, 12 months ago by doubleedesign.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You could use the same tactic wpautop() uses to avoid certain elements. (if you’re curious, review the source code to see how this is done) Instead of generating H3 tags, generate a placeholder comment like <!-- myH3 -->. wpautop should ignore this unless there are br tags or newlines involved. In a filter callback after wpautop runs, replace the placeholders with true H3 tags.

    Thread Starter doubleedesign

    (@doubleedesign)

    Thank you, I hadn’t thought of that! I haven’t tried it out yet but will do.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘wpautop breaking sprintf() output in a shortcode’ is closed to new replies.