logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$50
How to Limit Character Length in BuddyPress Function Output

BuddyPress has a function (bp_member_profile_data) that pulls profile data into a bp_has_members loop. The section looks like this in my current template:

<?php
bp_member_profile_data( 'field=Organization' );
bp_member_profile_data( 'field=Brief Biography' );
?>


I'd like to be able to limit the length of what the function echoes in the template files.

Ideally it would work for any field and to a different number of words in different places if possible.

Thanks very much.

This question has been answered.

cwulff | 12/11/11 at 12:22pm Edit


(8) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    12/11/11
    12:33pm
    John Cotton says:

    Well this will do it for you:


    function filter_profile_data( $data ) {
    return substr( $data, 0, 100);
    }
    add_filter( 'bp_get_member_profile_data', 'filter_profile_data' );


    Change the 100 to the length you want...

    Previous versions of this answer: 12/11/11 at 12:33pm

  • avatar
    Last edited:
    12/11/11
    1:55pm
    Eli Scheetz says:

    remove the line that says

    echo  substr( $data, 0, $len);

    that line was for outputting the given value truncated by characters (not words).

  • avatar
    Last edited:
    12/11/11
    12:42pm
    John Cotton says:

    I just had a quick step through the code to see if there was a hook that had field type, so that you could adjust the length according to what was being requested, but I can't see one.

    So the alternative would be to construct your own wrapper function, a bit like this:


    function filtered_profile_data( $args ) {

    $data = bp_get_member_profile_data( $args );

    $defaults = array(
    'field' => false, // Field name
    'user_id' => $user_id
    );

    $r = wp_parse_args( $args, $defaults );
    extract( $r, EXTR_SKIP );

    switch($field) {
    case 'Organization':
    $len = 50;
    break;
    default:
    $len = 100;
    break;
    }

    echo substr( $data, 0, $len);
    }



    Your code would then become:


    <?php

    filtered_profile_data( 'field=Organization' );

    filtered_profile_data( 'field=Brief Biography' );

    ?>


    Of course, you could make the $len logic much more complicated if you wanted...

  • avatar
    Last edited:
    12/11/11
    12:51pm
    cwulff says:

    Hi John,

    Oops... I forgot to change that to word count, not character, with a trailing ellipsis.
    (Just looked at my example and someone was playing with bras, which was supposed to be brash humour, but got cut off. Better stick to words.)

    Code seems to be working though.

  • avatar
    Last edited:
    12/11/11
    1:02pm
    John Cotton says:

    Words rather than characters?

    Well, assuming you are going with the wrapper function you could do this at the end:


    $words = array_slice( explode( ' ', $data), 0, $len );
    echo implode( ' ', $words );

  • avatar
    Last edited:
    12/11/11
    1:16pm
    John Cotton says:

    PS - Obviously you need to make $len the number of words, not the number of characters....

  • avatar
    Last edited:
    12/11/11
    1:17pm
    cwulff says:

    Yeah, words not characters. Works perfectly for restricting the number of words. The weird thing is that it restrict Brief Biography by words, but the other by characters. It also shows five characters (first field), followed by five words (first field), followed by ten words (second field).

    I added it like this, so maybe I just misunderstood something:

    function filtered_profile_data( $args ) {

    $data = bp_get_member_profile_data( $args );

    $defaults = array(
    'field' => false, // Field name
    'user_id' => $user_id
    );

    $r = wp_parse_args( $args, $defaults );
    extract( $r, EXTR_SKIP );

    switch($field) {
    case 'Brief Biography':
    $len = 10;
    break;
    default:
    $len = 5;
    break;
    }

    echo substr( $data, 0, $len);

    $words = array_slice( explode( ' ', $data), 0, $len );

    echo implode( ' ', $words );
    }

  • avatar
    Last edited:
    12/11/11
    1:57pm
    cwulff says:

    Perfect.

This question has expired.



cwulff, Gabriel Reguly, Christianto, Julio Potier, Luis Abarca, Francisco Javier Carazo Gil voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.