2

How to add placeholder in buddypress registration?

Can we use field description as placeholder for input field?

In register.php under bp-template.These two lines of code make xprofile

$field_type = bp_xprofile_create_field_type( bp_get_the_profile_field_type() );
$field_type->edit_field_html();

How to edit them.Where these lines are connected.How to edit x-profile field.

Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
Bugfixer
  • 2,547
  • 2
  • 26
  • 42

3 Answers3

3

Or even better yet, combining these two answers. Simply add this to your functions.php:

function wp_add_placeholder($elements){
    $elements['placeholder'] = bp_get_the_profile_field_name();
    return $elements;
}
add_action('bp_xprofile_field_edit_html_elements','wp_add_placeholder');
1

There is always a better way to do it then editing the core:

function wp_add_placeholder($elements){
    $elements['placeholder'] = 'Placeholder';
    return $elements;
}
add_action('bp_xprofile_field_edit_html_elements','wp_add_placeholder');
Mark
  • 6,762
  • 1
  • 33
  • 50
tolginho
  • 583
  • 9
  • 12
0

just struggled with this issue and fastest thing i came up were override classes that handles different fields:

First you need to find class named BP_XProfile_Field_Type_Textbox (for textbox). In bp v2.0.2 it can be found on bp-xprofile/bp-xprofile-class.php:2358. Copy whole class in your functions and rename class name as you desire. let's say i renamed it as CUSTOM_BP_XProfile_Field_Type_Textbox. Inside this class there are function called public function edit_field_html( array $raw_properties = array() ).

Replace:

    $html = $this->get_edit_field_html_elements( array_merge(
        array(
            'type'  => 'text',
            'value' => bp_get_the_profile_field_edit_value(),
        ),
        $raw_properties
    ) );
?>
    <label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php esc_html_e( '(required)', 'buddypress' ); ?><?php endif; ?></label>
    <?php do_action( bp_get_the_profile_field_errors_action() ); ?>
    <input <?php echo $html; ?>>
<?php
}

With that:

$required = bp_get_the_profile_field_is_required() ? ' ' . esc_html__( '(required)', 'buddypress' ) : '';
    $html = $this->get_edit_field_html_elements( array_merge(
        array(
            'type'  => 'text',
            'value' => bp_get_the_profile_field_edit_value(),
                            'placeholder' => bp_get_the_profile_field_name() . $required
        ),
        $raw_properties
    ) );
?>
    <?php do_action( bp_get_the_profile_field_errors_action() ); ?>

    <input <?php echo $html; ?>>
<?php
}

Next thing you need to do is override function that hendles field classes. Copy function called bp_xprofile_get_field_types into your theme and rename it. let's say i renamed it as custom_bp_xprofile_get_field_types.

In fields array rename 'textbox' value from BP_XProfile_Field_Type_Textbox to CUSTOM_BP_XProfile_Field_Type_Textbox (the class you created and modified).

Finl thing you need to do is override function that print final result. Copy function called bp_xprofile_create_field_type into your theme and rename it. let's say i renamed it as custom_bp_xprofile_create_field_type.

In this function replace:

$field = bp_xprofile_get_field_types();

with:

$field = custom_bp_xprofile_get_field_types();

to use your modified field output

In register.php use just created new function instead of original buddypresses one so the final result will be:

$field_type = custom_bp_xprofile_create_field_type( bp_get_the_profile_field_type() );
$field_type->edit_field_html();

It is good to copy register.php to YOUR_THEME/buddypress/members/register.php to not lose your changes after updating the bp plugin.

If you like to modify other fields just rename field's class in classes array in custom_bp_xprofile_get_field_types and copy bp's class of that field and rename it as in fields array.

Hope that helps. There might be better way, but i didn't find any.