Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I use shortcode to output the following html in a variable, but there are too many redundant code such as br and p tags, how to remove them? Thanks!

HTML MAKEUP

$return_html.='<div class="portfolio-item" id="portfolio-'.$post->ID.'"><a class="overlay'.$class.'" href="'.$url.'" title="'.$post->post_title.'"><h3>'.$post->post_title.'</h3><p class="intro">'.van_truncate($post->post_content,100).'</p></a><div class="tools"><a href="'.$url.'" class="zoomin'.$class.'" title="'.$post->post_title.'">ZoomIn</a><a href="'.get_permalink($post->ID).'" class="info">Info</a></div>
<a href="'.get_permalink($post->ID).'" class="item"><img src="'.get_template_directory_uri().'/images/pixel.gif" data-url="'.$thumbnail_url[0].'" class="scrollLoading" /></a></div>';

Current shortcode function

add_shortcode('portfolios', 'van_portfolios_shortcode');
function van_portfolios_shortcode( $atts, $content) {
    extract(shortcode_atts(array(
        'number'=>'9',
        'slug'=>''
    ), $atts)); 
   $str=van_portfolios($slug,$number,false);
   return $str;
}

function van_process_shortcode($content) {
    global $shortcode_tags;
    // Backup current registered shortcodes and clear them all out
    $orig_shortcode_tags = $shortcode_tags;
    $shortcode_tags = array();
    add_shortcode('portfolios', 'van_portfolios_shortcode');

    // Do the shortcode (only the one above is registered)
    $content = do_shortcode($content);
    // Put the original shortcodes back
    $shortcode_tags = $orig_shortcode_tags;
    return $content;
}
add_filter('the_content', 'van_process_shortcode', 7);

Correct makeup is

<div class="portfolio-item">
           <a class="overlay" href="#">
             <h3>...</h3>
             <p>...</p>
           </a>
           <div class="tools"><a href="#" class="zoomin" rel="lightbox">ZoomIn</a><a href="#" class="info">Info</a></div>
           <a href="#" class="item">...</a>
         </div>

Output:

<div class="portfolio-item">
           <a class="overlay" href="#">
             <br /><!--This <br />is redundant code-->
             <h3>...</h3>
             <p>...</p><p><!--This <p> is redundant code-->
           </a>
           <div class="tools"><a href="#" class="zoomin" rel="lightbox">ZoomIn</a><a href="#" class="info">Info</a></div>
           <p><!--This <p> is redundant code--><a href="#" class="item">...</a>
         </div>
share|improve this question

1 Answer 1

This is because the content of a post - and therefore your shortcode - is run through the wpautop() function which generates the <p> and <br /> tags in order to space out the post properly.

Shortcodes are run at priority 11, which is after wpautop() on priority 10. We can change the piority that this shortcode is run before wpautop():

// This will do nothing but will allow the shortcode to be stripped
add_shortcode( 'foobar', 'shortcode_foobar' );

// Actual processing of the shortcode happens here
function foobar_run_shortcode( $content ) {
    global $shortcode_tags;

    // Backup current registered shortcodes and clear them all out
    $orig_shortcode_tags = $shortcode_tags;
    remove_all_shortcodes();

    add_shortcode( 'foobar', 'shortcode_foobar' );

    // Do the shortcode (only the one above is registered)
    $content = do_shortcode( $content );

    // Put the original shortcodes back
    $shortcode_tags = $orig_shortcode_tags;

    return $content;
}

add_filter( 'the_content', 'foobar_run_shortcode', 7 );

Code thanks to @Viper007Bond [source]. Remember to replace the function and filter names with ones relating to your shortcode!

share|improve this answer
    
Thank you so much.I have corrected as your method and my codes is above, but it still generates the P and BR. –  BadJohnny Dec 21 '12 at 6:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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