Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extend BuddyPress User Contact Form
/**
* BuddyPress User Contact Form extension.
*/
class BPUCF_Custom_Extend {
public function __construct() {
$this->setup();
}
public function setup() {
add_filter( 'bpucf_form_fields', array( $this, 'add_fields' ) );
// Currently, the form does not support rendering select boxes, we wil write our own.
add_filter( 'bpucf_field_html', array( $this, 'render_fields' ), 10, 3 );
// message append.
add_filter( 'bpucf_message_content', array( $this, 'add_to_message' ), 10, 2 );
}
/**
* Add extra fields.
*
* @param array $fields fields.
*
* @return array
*/
public function add_fields( $fields ) {
$fields['phone'] = array(
'name' => 'phone',
'type' => 'text',
'label' => __( 'Phone Number', 'buddypress-user-contact-form' ),
'required' => true,
'position' => 4,
);
$fields['country'] = array(
'name' => 'country',
'type' => 'select',
'label' => __( 'Country', 'buddypress-user-contact-form' ),
'required' => true,
'position' => 5,
'options' => array(
'us' => 'United States',
'uk' => 'United Kingdom',
'in' => 'India',
'other' => 'Other',
),
);
$fields['adults'] = array(
'name' => 'adults',
'type' => 'select',
'label' => __( 'Adults', 'buddypress-user-contact-form' ),
'required' => true,
'position' => 5,
'options' => array(
1 => 'One adult',
2 => 'Two adult',
),
);
return $fields;
}
/**
* Since the form(1.0.3-1.0.3) does not support select boxes, we add custom code for rendering it.
*
* @param string $html Generated html for the field.
* @param array $field field details.
* @param string $type field type.
*
* @return string
*/
public function render_fields( $html , $field, $type ) {
if ( ! empty( $html ) || 'select' !== $type ) {
return $html;
}
$label = esc_html( $field['label'] );
$name = esc_attr( $field['name'] );
$required = $field['required'] ? '<span class="bpucf-field-required">*</span>' : '';
$options = isset( $field['options'] ) ? $field['options'] : array();
$html = "<label> {$label} {$required}";
$html .= sprintf( "<select name='%s'>", esc_attr( $name ) );
foreach ( $options as $key => $value ) {
$html .= sprintf( "<option value='%s'>%s</option>", esc_attr( $key ), esc_html( $value ) );
}
$html .= '</select></label>';
return $html;
}
/**
* Add info to the message.
*
* @param string $message message.
* @param string $context context.
*
* @return string
*/
public function add_to_message( $message, $context ) {
// in our case, we do not worry about content.
// Let us append the extra fields to the message.
if ( isset( $_POST['phone'] ) ) {
$message .= "\r\n Phone Number:" . esc_html( $_POST['phone'] );
}
if ( isset( $_POST['adults'] ) ) {
$message .= "\r\n Adults:" . esc_html( $_POST['adults'] );
}
if ( isset( $_POST['country'] ) ) {
$fields = bpucf_get_form_fields();
$countries = $fields['country']['options'];
$country = isset( $countries[ $_POST['country'] ] ) ? $countries[ $_POST['country'] ] : 'Not Provided';
$message .= "\r\n Country:" . esc_html( $country );
}
return $message;
}
}
new BPUCF_Custom_Extend();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.