Skip to:
Content
Pages
Categories
Search
Top
Bottom

adding html attributes to Xprofile fields – code check


  • AM77
    Participant

    @andy277

    I need to add some html attributes to some Xprofile fields such as max-length. I was trying to find a filter so I could use str_replace to add attributes, but I eventually found the filter bp_xprofile_field_edit_html_elements in bp-xprofile/classes/class-bp-xprofile-field-type.php.
    I’ve now got this code which works, but I’m not great with PHP so just checking if this is the best way to achieve this:

    function hp_xprofile_add_attributes($r){
    
    	$field_name = bp_get_the_profile_field_name();
    	
    	if( $field_name == 'Name' ) {
    
    		$new_attribute['maxlength']= '20';		 
    		$attributes = array_merge($new_attribute, $r);
    
    	} elseif( $field_name == 'Short Description') {
    
    		$new_attribute['maxlength']= '160';		 
    		$attributes = array_merge($new_attribute, $r);
    		
    	} else {
    		
    		$attributes = $r;
    	}
    		
        return $attributes;
    	
    }
    add_filter('bp_xprofile_field_edit_html_elements', 'hp_xprofile_add_attributes', 11);

    Any help appreciated.

Viewing 1 replies (of 1 total)

  • Henry Wright
    Moderator

    @henrywright

    I don’t think you need $field_name = bp_get_the_profile_field_name(); because you’re passing $r to your function. So you can just use $r['name'].

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.
Skip to toolbar