WordPress.org

Ready to get started?Download WordPress

Forums

Buddypress Xprofile Custom Fields Type
[resolved] Make email be a mailto link (12 posts)

  1. David Hunt
    Member
    Posted 1 year ago #

    Great idea for a plugin! I was just thinking today how limited the field types in extended profiles are.

    One question: is it possible to make the email field output a mailto: link? Right now it just links to a search.

    http://wordpress.org/extend/plugins/buddypress-xprofile-custom-fields-type/

  2. donmik
    Member
    Plugin Author

    Posted 1 year ago #

    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?

  3. David Hunt
    Member
    Posted 1 year ago #

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

  4. David Hunt
    Member
    Posted 1 year ago #

    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.

  5. donmik
    Member
    Plugin Author

    Posted 1 year ago #

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

  6. David Hunt
    Member
    Posted 1 year ago #

    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;
    }
  7. donmik
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  8. donmik
    Member
    Plugin Author

    Posted 1 year ago #

    I mark this topic as resolved.

  9. David Hunt
    Member
    Posted 1 year ago #

    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

  10. David Hunt
    Member
    Posted 1 year ago #

    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;
    }
  11. donmik
    Member
    Plugin Author

    Posted 1 year ago #

    I'm glad that everything runs smoothly!

  12. companyjuice
    Member
    Posted 1 year ago #

    Very cool. I actually was wondering why any profile field that had an "http://" or "https://" 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!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags