Skip to:
Content
Pages
Categories
Search
Top
Bottom

Set Excerpt Length of Group Description


  • Quint
    Participant

    @qrahaman

    Hello,

    The bp_create_excerpt() (in bp-core-template.php, line 365) sets the length of the description to 225 characters. I don’t want to modify the core file. Can someone tell me how to properly modify this in the functions.php file? Thanks!

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

  • meg@info
    Participant

    @megainfo

    Hi @qrahaman,

    i dont understand exactly what you want,if you want to excerpt of the description of group in the group page. this code work fine :

    function bp_excerpt_group_description( $description ) {

    // your exceprt code
    $length = 30;
    $description = substr($description,0,$length);
    return $description;
    }

    add_filter( 'bp_get_group_description', 'bp_excerpt_group_description');


    Quint
    Participant

    @qrahaman

    @megainfo,

    Sorry, I most certainly wasn’t clear. What you provided works in the Group’s main page where the description length is fine (which is good, since I may want to modify that later).

    What I wanted to do was specify the length of excerpt on the group directory page. So, given what you laid out, is there a hook that could be applied for that location? The groups loop is calling bp_group_description_excerpt().

    Hopefully, that’s clearer. Thanks again.


    meg@info
    Participant

    @megainfo

    hi @qrahaman , try this,

    function bp_excerpt_group_description( $description ) {

    // your exceprt code
    $length = 30;
    $description = substr($description,0,$length);
    return $description;
    }

    add_filter( ‘bp_get_group_description_excerpt’, ‘bp_excerpt_group_description’);


    Quint
    Participant

    @qrahaman

    @megainfo,

    Thanks. That does work to produce the excerpt at the right spot. However, it does not properly handle not cutting words in the middle nor putting the […] at the end. The following function in the core file (mentioned in my first comment) does that:

    `function bp_create_excerpt( $text, $length = 225, $options = array() ) {
    // Backward compatibility. The third argument used to be a boolean $filter_shortcodes
    $filter_shortcodes_default = is_bool( $options ) ? $options : true;

    $defaults = array(
    ‘ending’ => __( ‘ […]’, ‘buddypress’ ),
    ‘exact’ => false,
    ‘html’ => true,
    ‘filter_shortcodes’ => $filter_shortcodes_default
    );
    $r = wp_parse_args( $options, $defaults );
    extract( $r );

    // Save the original text, to be passed along to the filter
    $original_text = $text;

    // Allow plugins to modify these values globally
    $length = apply_filters( ‘bp_excerpt_length’, $length );
    $ending = apply_filters( ‘bp_excerpt_append_text’, $ending );

    // Remove shortcodes if necessary
    if ( !empty( $filter_shortcodes ) )
    $text = strip_shortcodes( $text );

    // When $html is true, the excerpt should be created without including HTML tags in the
    // excerpt length
    if ( !empty( $html ) ) {
    // The text is short enough. No need to truncate
    if ( mb_strlen( preg_replace( ‘//’, ”, $text ) ) <= $length ) {
    return $text;
    }

    $totalLength = mb_strlen( strip_tags( $ending ) );
    $openTags = array();
    $truncate = ”;

    // Find all the tags and put them in a stack for later use
    preg_match_all( ‘/(]*>)?([^]*)/’, $text, $tags, PREG_SET_ORDER );
    foreach ( $tags as $tag ) {
    // Process tags that need to be closed
    if ( !preg_match( ‘/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s’, $tag[2] ) ) {
    if ( preg_match( ‘/]*>/s’, $tag[0] ) ) {
    array_unshift( $openTags, $tag[2] );
    } else if ( preg_match(‘/]*>/s’, $tag[0], $closeTag ) ) {
    $pos = array_search( $closeTag[1], $openTags );
    if ( $pos !== false ) {
    array_splice( $openTags, $pos, 1 );
    }
    }
    }
    $truncate .= $tag[1];

    $contentLength = mb_strlen( preg_replace( ‘/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i’, ‘ ‘, $tag[3] ) );
    if ( $contentLength + $totalLength > $length ) {
    $left = $length – $totalLength;
    $entitiesLength = 0;
    if ( preg_match_all( ‘/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i’, $tag[3], $entities, PREG_OFFSET_CAPTURE ) ) {
    foreach ( $entities[0] as $entity ) {
    if ( $entity[1] + 1 – $entitiesLength <= $left ) {
    $left–;
    $entitiesLength += mb_strlen( $entity[0] );
    } else {
    break;
    }
    }
    }

    $truncate .= mb_substr( $tag[3], 0 , $left + $entitiesLength );
    break;
    } else {
    $truncate .= $tag[3];
    $totalLength += $contentLength;
    }
    if ( $totalLength >= $length ) {
    break;
    }
    }
    } else {
    if ( mb_strlen( $text ) <= $length ) {
    return $text;
    } else {
    $truncate = mb_substr( $text, 0, $length – mb_strlen( $ending ) );
    }
    }

    // If $exact is false, we can’t break on words
    if ( empty( $exact ) ) {
    $spacepos = mb_strrpos( $truncate, ‘ ‘ );
    if ( isset( $spacepos ) ) {
    if ( $html ) {
    $bits = mb_substr( $truncate, $spacepos );
    preg_match_all( ‘//’, $bits, $droppedTags, PREG_SET_ORDER );
    if ( !empty( $droppedTags ) ) {
    foreach ( $droppedTags as $closingTag ) {
    if ( !in_array( $closingTag[1], $openTags ) ) {
    array_unshift( $openTags, $closingTag[1] );
    }
    }
    }
    }
    $truncate = mb_substr( $truncate, 0, $spacepos );
    }
    }
    $truncate .= $ending;

    if ( $html ) {
    foreach ( $openTags as $tag ) {
    $truncate .= ”;
    }
    }

    return apply_filters( ‘bp_create_excerpt’, $truncate, $original_text, $length, $options );

    }
    add_filter( ‘bp_create_excerpt’, ‘stripslashes_deep’ );
    add_filter( ‘bp_create_excerpt’, ‘force_balance_tags’ );
    `

    So, using that function in functions.php in the following does not work (I think it’s similar to a circular reference error in Excel):

    add_filter( ‘bp_get_group_description_excerpt’, ‘bp_create_excerpt’);

    This results in the white screen of death, though it’s probably close. Anyway, thanks for your help.


    Joseph G.
    Participant

    @illumination2025

    Ever figure this one out? Thanks in advance. 🙂


    Quint
    Participant

    @qrahaman


    Joseph G.
    Participant

    @illumination2025

    Great… thanks, Quint!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Set Excerpt Length of Group Description’ is closed to new replies.
Skip to toolbar