Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to Limit Character Length in BuddyPress Function Output WordPress

  • SOLVED

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.

Answers (2)

2011-12-11

John Cotton answers:

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...


John Cotton comments:

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...


Christopher comments:

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.


John Cotton comments:

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 );


John Cotton comments:

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


Christopher comments:

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 );
}

2011-12-11

Eli Scheetz answers:

remove the line that says
echo substr( $data, 0, $len);
that line was for outputting the given value truncated by characters (not words).


Christopher comments:

Perfect.