WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to remove the automatic paragraph tag that goes around my excerpts, which are being outputted by a custom function. I'd really appreciate if you could tell me what to add to my code.

I'm using this custom function for excerpts:

  // Default length (by WordPress)
  public static $length = 25;

  // So you can call: my_excerpt('short');
  public static $types = array(
      'short' => 5,
      'regular' => 25,
      'long' => 100

  public static function length($new_length = 55) {
    Excerpt::$length = $new_length;
    add_filter('excerpt_length', 'Excerpt::new_length');
  public static function new_length() {
    if( isset(Excerpt::$types[Excerpt::$length]) )
      return Excerpt::$types[Excerpt::$length];
      return Excerpt::$length;
  public static function output() {

// An alias to the class
function my_excerpt($length = 25) {

and calling it using

<?php my_excerpt('short'); ?>
share|improve this question
up vote 0 down vote accepted

If I'm understanding your question correctly, I think adding remove_filter( 'the_excerpt', 'wpautop' ); around when you have add_filter('excerpt_length', 'Excerpt::new_length'); does what you're looking for.

share|improve this answer

I know two good techniques that work fine with shortcodes and should work with excerpts as well.

You're able to parse excerpt's content using:

function my_excerpt( $content ) {

    /* Parse nested shortcodes and add formatting. */
    $content = trim( wpautop( do_shortcode( $content ) ) );

    /* Remove '</p>' from the start of the string. */
    if ( substr( $content, 0, 4 ) == '</p>' )
        $content = substr( $content, 4 );

    /* Remove '<p>' from the end of the string. */
    if ( substr( $content, -3, 3 ) == '<p>' )
        $content = substr( $content, 0, -3 );

    /* Remove any instances of '<p></p>'. */
    $content = str_replace( array( '<p></p>' ), '', $content );

    return $content;

In addition there is technique using [raw] shortcode:

function my_formatter($content) {
    $new_content = '';
    $pattern_full = '{(\[raw\].*?\[/raw\])}is';
    $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
    $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

    foreach ($pieces as $piece) {
        if (preg_match($pattern_contents, $piece, $matches)) {
            $new_content .= $matches[1];
        } else {
            $new_content .= wptexturize(wpautop($piece));

    return $new_content;

remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');

add_filter('the_content', 'my_formatter', 99);

I hope it helps.

If you want us to help you with your function - it is always a good idea to say what is wrong with the function in the first place?

share|improve this answer
There's nothing wrong with the function. Wordpress automatically adds paragraph tags to excerpts, I want to remove the paragraph tags for this excerpt. – AndrettiMilas Sep 1 '11 at 1:18

Your Answer


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.