Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply To: How to ignore line breaks for bbpress meta description?

@robin-w

Moderator

untested, but this should take out newlines and change the excerpt length to 150 – just change the 150 in the code to whatever length you want.

so firstly you need to alter the default in wordpress which is 55 so

add_filter( 'excerpt_length', 'rew_change_default_length' ) ;

function rew_change_default_length () {
    return 150;
}

then alter it in the bbpress function, and change to take out line breaks

add_filter ('bbp_get_topic_excerpt' , 'rew_remove_line_breaks', 10 , 3) ;

function rew_remove_line_breaks ($excerpt, $topic_id, $length){
		$topic_id = bbp_get_topic_id( $topic_id );
		//change length here
		$length   = 150 ;
		$excerpt  = get_post_field( 'post_excerpt', $topic_id );

		if ( empty( $excerpt ) ) {
			$excerpt = bbp_get_topic_content( $topic_id );
		}

		$excerpt = trim( strip_tags( $excerpt ) );
		
		//take out line breaks
		$excerpt = preg_replace("/\r|\n/", "", $excerpt);

		// Multibyte support
		if ( function_exists( 'mb_strlen' ) ) {
			$excerpt_length = mb_strlen( $excerpt );
		} else {
			$excerpt_length = strlen( $excerpt );
		}

		if ( ! empty( $length ) && ( $excerpt_length > $length ) ) {
			$excerpt  = mb_substr( $excerpt, 0, $length - 1 );
			$excerpt .= '…';
		}
		
		
		// Filter & return
		return apply_filters( 'rew_get_topic_excerpt', $excerpt, $topic_id, $length );
}

Put this in your child theme’s function file –

ie wp-content/themes/%your-theme-name%/functions.php

where %your-theme-name% is the name of your theme

or use

Code Snippets

Skip to toolbar