Skip to content

Instantly share code, notes, and snippets.

@jlengstorf
Last active September 10, 2018 01:50
Show Gist options
  • Select an option

  • Save jlengstorf/5370457 to your computer and use it in GitHub Desktop.

Select an option

Save jlengstorf/5370457 to your computer and use it in GitHub Desktop.

Revisions

  1. Jason Lengstorf revised this gist Apr 12, 2013. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions removecrappymarkup.php
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,16 @@
    <?php

    /**
    * Removes mismatched </p> and <p> tags from the beginning and end of a snippet.
    *
    * @author Jason Lengstorf <jason@copterlabs.com>
    */
    function copter_remove_crappy_markup( $string )
    {
    $patterns = array(
    '#^\s*</p>#',
    '#<p>\s*$#'
    );

    $replacements = array('', '');

    return preg_replace($patterns, '', $string);
    }
  2. Jason Lengstorf revised this gist Apr 12, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions removecrappymarkup.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <?php

    function copter_remove_crappy_markup( $string )
    {
    $patterns = array(
  3. Jason Lengstorf created this gist Apr 12, 2013.
    41 changes: 41 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    Remove Crappy Markup from WordPress Shortcodes
    ==============================================

    When using shortcodes in WordPress like so:

    [shortcode]

    Content goes here...

    [/shortcode]

    And assuming that the shortcode wraps your content with another element, sort of like this:

    function my_shortcode( $attr, $content )
    {
    return '<div class="my_shortcode">' . $content . '</div>';
    }
    add_shortcode('shortcode', 'my_shortcode');

    The built-in WordPress `wpautop` filter will add junk markup to your output:

    <div class="my_shortcode">
    </p>
    <p>Content goes here...</p>
    <p>
    </div>

    Adding `copter_remove_crappy_markup()` to the shorcode will clean up the output:

    function my_shortcode( $attr, $content )
    {
    $clean = copter_remove_crappy_markup($content);
    return '<div class="my_shortcode">' . $clean . '</div>';
    }
    add_shortcode('shortcode', 'my_shortcode');

    Resulting in:

    <div class="my_shortcode">
    <p>Content goes here...</p>
    </div>
    11 changes: 11 additions & 0 deletions removecrappymarkup.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    function copter_remove_crappy_markup( $string )
    {
    $patterns = array(
    '#^\s*</p>#',
    '#<p>\s*$#'
    );

    $replacements = array('', '');

    return preg_replace($patterns, '', $string);
    }