Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author donmik

    (@atallos)

    Right now, a buddypress filter transform the email field into a mailto: link.

    It is line 27 in buddypress/bp-xprofile/bp-xprofile-filters.php.
    add_filter( ‘bp_get_the_profile_field_value’, ‘make_clickable’ );

    This line apply a filter to the email field and the url field too and make them clickable links.

    I assume it’s not working in your buddypress installation, isn’t it?

    Thread Starter David Hunt

    (@dvd3141)

    Hmm, that is odd. BuddyPress (or WordPress?) is converting it to a search link (that is, the href is https://example.com/members/?s=user%40example.com). I am not sure why.

    Thread Starter David Hunt

    (@dvd3141)

    Ah, I think I see why. Inside bp-xprofile-filters.php, there is also the following filter:

    add_filter( 'bp_get_the_profile_field_value', 'xprofile_filter_link_profile_data', 9, 2 );

    This function xprofile_filter_link_profile_data is defined around line 156 in the same file. I changed the following part of that function:

    if ( preg_match( '@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $value ) ) {
    				$new_values[] = make_clickable( $value );

    to this:

    if ( preg_match( '@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $value ) || (preg_match("/^[^@]*@[^@]*\.[^@]*$/", $value)) ) {
    				$new_values[] = make_clickable( $value );

    So now it checks if $value is a URL or an email address, and if it is either, calls make_clickable.

    But I am not sure why my BuddyPress didn’t already do this.

    Plugin Author donmik

    (@atallos)

    It’s really weird… In my version of BuddyPress it works.

    If you want to let your buddypress clean, maybe you prefer put this code in your functions.php. Remove the original filter with remove_filter and add another filter with your custom code.

    I will investigate this week if I found why it is not working in your case. I’ll be in touch if I find something…

    Thread Starter David Hunt

    (@dvd3141)

    Good idea about putting them in my functions.php. I decided to stop it converting things to search terms too (but still allow people to enter comma-separated email addresses).

    So in my functions.php I have:

    add_action('init', 'make_xprofile_emails_mailto');
    
    function make_xprofile_emails_mailto() {
    	remove_filter( 'bp_get_the_profile_field_value', 'xprofile_filter_link_profile_data', 9 );
    	add_filter( 'bp_get_the_profile_field_value', 'my_xprofile_filter_link_profile_data', 10, 2);
    }
    
    function my_xprofile_filter_link_profile_data( $field_value, $field_type = 'textbox' ) {
    	if ( 'datebox' == $field_type )
    		return $field_value;
    
    	if ( !strpos( $field_value, ',' ) && ( count( explode( ' ', $field_value ) ) > 5 ) )
    		return $field_value;
    
    	$values = explode( ',', $field_value );
    
    	if ( !empty( $values ) ) {
    		foreach ( (array) $values as $value ) {
    			$value = trim( $value );
    			$new_values[] = make_clickable( $value );
    		}
    		$values = implode( ', ', $new_values );
    	}
    	return $values;
    }
    Plugin Author donmik

    (@atallos)

    I’ve found the bug (or feature…xD).

    I was removing the filter in my functions.php and I can’t see the links from buddypress.

    To solve this, I have just release a new version who remove the filter and add a new filter who do exactly the same but excluding new fields type created by the plugin.

    I hope it works for you too.

    Plugin Author donmik

    (@atallos)

    I mark this topic as resolved.

    Thread Starter David Hunt

    (@dvd3141)

    Sorry to re-open this … I just found time to upgrade to your new version 1.4.

    I don’t quite understand what has changed. Now my custom code above does nothing. How can I make it so that:

    1. Emails and URLs get automatically linked (with emails linked to mailto:)
    2. Other, plain text profile fields do not get linked.

    Thanks for your help so far!

    David

    Thread Starter David Hunt

    (@dvd3141)

    Okay, I dug into your plugin and found the name of the new filter you added. So to get the behaviour I want, in my functions.php I need to do this:

    add_action('init', 'my_xprofile_links');
    function my_xprofile_links() {
    	if (has_filter('bp_get_the_profile_field_value', 'xprofile_filter_link_profile_data')) {
    		remove_filter( 'bp_get_the_profile_field_value', 'xprofile_filter_link_profile_data', 9 );
    	}
    	if (has_filter('bp_get_the_profile_field_value', 'bxcft_xprofile_filter_link_profile_data')) {
    		remove_filter( 'bp_get_the_profile_field_value', 'bxcft_xprofile_filter_link_profile_data', 9);
    	}
    	add_filter( 'bp_get_the_profile_field_value', 'my_xprofile_filter_link_profile_data', 10, 2);
    }
    
    function my_xprofile_filter_link_profile_data( $field_value, $field_type = 'textbox' ) {
    	if ( 'datebox' == $field_type )
    		return $field_value;
    
    	if ( !strpos( $field_value, ',' ) && ( count( explode( ' ', $field_value ) ) > 5 ) )
    		return $field_value;
    
    	$values = explode( ',', $field_value );
    
    	if ( !empty( $values ) ) {
    		foreach ( (array) $values as $value ) {
    			$value = trim( $value );
    			$new_values[] = make_clickable( $value );
    		}
    		$values = implode( ', ', $new_values );
    	}
    	return $values;
    }
    Plugin Author donmik

    (@atallos)

    I’m glad that everything runs smoothly!

    Very cool. I actually was wondering why any profile field that had an “https://&#8221; or “https://&#8221; was generating a full <a href> tag when I already had a remove_filter( 'bp_get_the_profile_field_value', 'xprofile_filter_link_profile_data', 9, 2 ); in my bp-custom.php file. Turns out (after reading this thread) that I needed to also remove the make_clickable filter. Here’s what I’m using now:

    // remove auto-linking and p tags on profile fields
    function remove_links(){
    	remove_filter( 'bp_get_the_profile_field_value', 'xprofile_filter_link_profile_data', 9, 2 );
    	remove_filter( 'bp_get_the_profile_field_value', 'wpautop');
    	remove_filter( 'bp_get_the_profile_field_value', 'make_clickable' );
    }
    add_action( 'bp_init', 'remove_links' );

    Thanks everyone!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘[Plugin: Buddypress Xprofile Custom Fields Type] Make email be a mailto link’ is closed to new replies.