Skip to content

Instantly share code, notes, and snippets.

@lots0logs
Last active November 10, 2023 14:32
Show Gist options
  • Save lots0logs/529bce32107e26763e13 to your computer and use it in GitHub Desktop.
Save lots0logs/529bce32107e26763e13 to your computer and use it in GitHub Desktop.
Divi Contact Form Module Customization
<?php
function my_et_theme_setup() {
if ( class_exists( 'ET_Builder_Module_Contact_Form' ) ) {
get_template_part( 'my-main-modules' );
$et_pb_contact = new My_ET_Builder_Module_Contact_Form();
remove_shortcode('et_pb_contact_form');
add_shortcode('et_pb_contact_form', array( $et_pb_contact, '_shortcode_callback' ) );
}
}
add_action( 'wp', 'my_et_theme_setup', 99 );
?>
<?php
class My_ET_Builder_Module_Contact_Form extends ET_Builder_Module_Contact_Form {
function shortcode_callback( $atts, $content = null, $function_name ) {
$module_id = $this->shortcode_atts['module_id'];
$module_class = $this->shortcode_atts['module_class'];
$captcha = $this->shortcode_atts['captcha'];
$email = $this->shortcode_atts['email'];
$title = $this->shortcode_atts['title'];
$form_background_color = $this->shortcode_atts['form_background_color'];
$input_border_radius = $this->shortcode_atts['input_border_radius'];
$button_custom = $this->shortcode_atts['custom_button'];
$custom_icon = $this->shortcode_atts['button_icon'];
$module_class = ET_Builder_Element::add_module_order_class( $module_class, $function_name );
if ( '' !== $form_background_color ) {
ET_Builder_Element::set_style( $function_name, array(
'selector' => '%%order_class%% .input',
'declaration' => sprintf(
'background-color: %1$s;',
esc_html( $form_background_color )
),
) );
}
if ( ! in_array( $input_border_radius, array( '', '0' ) ) ) {
ET_Builder_Element::set_style( $function_name, array(
'selector' => '%%order_class%% .input',
'declaration' => sprintf(
'-moz-border-radius: %1$s; -webkit-border-radius: %1$s; border-radius: %1$s;',
esc_html( et_builder_process_range_value( $input_border_radius ) )
),
) );
}
$et_pb_contact_form_num = $this->shortcode_callback_num();
$et_error_message = '';
$et_contact_error = false;
$contact_email = isset( $_POST['et_pb_contact_email_' . $et_pb_contact_form_num] ) ? sanitize_email( $_POST['et_pb_contact_email_' . $et_pb_contact_form_num] ) : '';
if ( isset( $_POST['et_pb_contactform_submit_' . $et_pb_contact_form_num] ) ) {
if ( empty( $_POST['et_pb_contact_name_' . $et_pb_contact_form_num] ) || empty( $contact_email ) || empty( $_POST['et_pb_contact_message_' . $et_pb_contact_form_num] ) ) {
$et_error_message .= sprintf( '<p>%1$s</p>', esc_html__( 'Make sure you fill all fields.', 'et_builder' ) );
$et_contact_error = true;
}
if ( 'on' === $captcha && ( ! isset( $_POST['et_pb_contact_captcha_' . $et_pb_contact_form_num] ) || empty( $_POST['et_pb_contact_captcha_' . $et_pb_contact_form_num] ) ) ) {
$et_error_message .= sprintf( '<p>%1$s</p>', esc_html__( 'Make sure you entered the captcha.', 'et_builder' ) );
$et_contact_error = true;
}
if ( ! is_email( $contact_email ) ) {
$et_error_message .= sprintf( '<p>%1$s</p>', esc_html__( 'Invalid Email.', 'et_builder' ) );
$et_contact_error = true;
}
} else {
$et_contact_error = true;
}
// generate digits for captcha
$et_pb_first_digit = rand( 1, 15 );
$et_pb_second_digit = rand( 1, 15 );
if ( ! $et_contact_error && isset( $_POST['_wpnonce-et-pb-contact-form-submitted'] ) && wp_verify_nonce( $_POST['_wpnonce-et-pb-contact-form-submitted'], 'et-pb-contact-form-submit' ) ) {
$et_email_to = '' !== $email
? $email
: get_site_option( 'admin_email' );
$et_site_name = get_option( 'blogname' );
$contact_name = stripslashes( sanitize_text_field( $_POST['et_pb_contact_name_' . $et_pb_contact_form_num] ) );
$headers[] = "From: \"{$contact_name}\" <mail@{$_SERVER['HTTP_HOST']}>";
$headers[] = "Reply-To: \"{$contact_name}\" <{$contact_email}>";
$et_email_subject = sprintf( __( 'New Message From %1$s%2$s', 'et_builder' ),
sanitize_text_field( html_entity_decode( $et_site_name ) ),
( '' !== $title ? sprintf( _x( ' - %s', 'contact form title separator', 'et_builder' ), sanitize_text_field( html_entity_decode( $title ) ) ) : '' )
);
$et_email_message = stripslashes( wp_strip_all_tags( $_POST['et_pb_contact_message_' . $et_pb_contact_form_num] ) );
wp_mail(
apply_filters( 'et_pb_comment_form_to', $et_email_to, $contact_name, $contact_email ),
apply_filters( 'et_pb_contact_form_subject', $et_email_subject, $contact_name, $contact_email ),
apply_filters( 'et_pb_contact_form_message', $et_email_message, $contact_name, $contact_email ),
apply_filters( 'et_pb_contact_form_headers', $headers, $contact_name, $contact_email )
);
$et_error_message = sprintf( '<p>%1$s</p>', esc_html__( 'Thanks for contacting us', 'et_builder' ) );
}
$form = '';
$name_label = __( 'Name', 'et_builder' );
$email_label = __( 'Email Address', 'et_builder' );
$message_label = __( 'Message', 'et_builder' );
$et_pb_captcha = sprintf( '
<div class="et_pb_contact_right">
<p class="clearfix">
<span class="et_pb_contact_captcha_question">%1$s</span> = <input type="text" size="2" class="input et_pb_contact_captcha" data-first_digit="%3$s" data-second_digit="%4$s" value="" name="et_pb_contact_captcha_%2$s">
</p>
</div> <!-- .et_pb_contact_right -->',
sprintf( '%1$s + %2$s', esc_html( $et_pb_first_digit ), esc_html( $et_pb_second_digit ) ),
esc_attr( $et_pb_contact_form_num ),
esc_attr( $et_pb_first_digit ),
esc_attr( $et_pb_second_digit )
);
if ( $et_contact_error )
$form = sprintf( '
<div class="et_pb_contact">
<form class="et_pb_contact_form clearfix" method="post" action="%1$s">
<div class="et_pb_contact_left">
<p class="clearfix">
<label for="et_pb_contact_name_%13$s" class="et_pb_contact_form_label">%2$s</label>
<input type="text" id="et_pb_contact_name_%13$s" class="input et_pb_contact_name" value="%3$s" name="et_pb_contact_name_%13$s">
</p>
<p class="clearfix">
<label for="et_pb_contact_email_%13$s" class="et_pb_contact_form_label">%4$s</label>
<input type="text" id="et_pb_contact_email_%13$s" class="input et_pb_contact_email" value="%5$s" name="et_pb_contact_email_%13$s">
</p>
</div> <!-- .et_pb_contact_left -->
<div class="clear"></div>
<p class="clearfix">
<label for="et_pb_contact_message_%13$s" class="et_pb_contact_form_label">%7$s</label>
<textarea name="et_pb_contact_message_%13$s" id="et_pb_contact_message_%13$s" class="et_pb_contact_message input">%8$s</textarea>
</p>
<input type="hidden" value="et_contact_proccess" name="et_pb_contactform_submit_%13$s">
<button type="submit" class="et_pb_contact_submit et_pb_button%12$s"%11$s>%9$s</button>
%6$s
%10$s
</form>
</div> <!-- .et_pb_contact -->',
esc_url( get_permalink( get_the_ID() ) ),
$name_label,
( isset( $_POST['et_pb_contact_name_' . $et_pb_contact_form_num] ) ? esc_attr( $_POST['et_pb_contact_name_' . $et_pb_contact_form_num] ) : $name_label ),
$email_label,
( isset( $_POST['et_pb_contact_email_' . $et_pb_contact_form_num] ) ? esc_attr( $_POST['et_pb_contact_email_' . $et_pb_contact_form_num] ) : $email_label ),
( 'on' === $captcha ? $et_pb_captcha : '' ),
$message_label,
( isset( $_POST['et_pb_contact_message_' . $et_pb_contact_form_num] ) ? esc_attr( $_POST['et_pb_contact_message_' . $et_pb_contact_form_num] ) : $message_label ),
__( 'Submit', 'et_builder' ),
wp_nonce_field( 'et-pb-contact-form-submit', '_wpnonce-et-pb-contact-form-submitted', true, false ),
'' !== $custom_icon && 'on' === $button_custom ? sprintf(
' data-icon="%1$s"',
esc_attr( et_pb_process_font_icon( $custom_icon ) )
) : '',
'' !== $custom_icon && 'on' === $button_custom ? ' et_pb_custom_button_icon' : '',
esc_attr( $et_pb_contact_form_num )
);
$output = sprintf( '
<div id="%4$s" class="et_pb_module et_pb_contact_form_container clearfix%5$s">
%1$s
<div class="et-pb-contact-message">%2$s</div>
%3$s
</div> <!-- .et_pb_contact_form_container -->
',
( '' !== $title ? sprintf( '<h1 class="et_pb_contact_main_title">%1$s</h1>', esc_html( $title ) ) : '' ),
'' !== $et_error_message ? $et_error_message : '',
$form,
( '' !== $module_id
? esc_attr( $module_id )
: esc_attr( 'et_pb_contact_form_' . $et_pb_contact_form_num )
),
( '' !== $module_class ? sprintf( ' %1$s', esc_attr( $module_class ) ) : '' )
);
return $output;
}
}
new My_ET_Builder_Module_Contact_Form;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment