Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to ignore line breaks for bbpress meta description?

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

  • Robin W
    Moderator

    @robin-w

    so are you using an seo plugin ?


    elliesatt
    Participant

    @zee300

    Yes, RankMath. Is that what’s generating the meta description? I though (perhaps naively) that bbpress did that.


    Robin W
    Moderator

    @robin-w

    bbpress doesn’t


    elliesatt
    Participant

    @zee300

    Ah OK – I’ve just found out that RankMath simply extracts the bbpress topic excerpt and uses that for the meta description, i.e. bbp_topic_excerpt()

    Is there a way to make bbp_topic_excerpt() ignore line breaks and increase character limit?


    Robin W
    Moderator

    @robin-w

    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

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar