Easy Contact Shortcode

Below, you’ll find a detailed guide on how to add the Easy Contact Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Easy Contact Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Easy Contact Plugin and the shortcodes it provides:

Plugin Icon
Easy Contact

"Easy Contact is a user-friendly WordPress plugin that simplifies communication by adding an efficient contact form to your website. It's ideal for enhancing user interaction and engagement."

★★★★✩ (3) Active Installs: 2000+ Tested with: 2.6.1 PHP Version: false
Included Shortcodes:
  • [easy-contact]

Easy Contact [easy-contact] Shortcode

The ‘easy-contact’ shortcode from the Easy Contact plugin initiates a contact form on your WordPress site. It collects user input, validates it, and sends an email to the specified recipient. Upon form submission, the shortcode function ‘ec_shortcode’ processes the data. It checks if the input is valid, constructs an email message, and sends it. If the input is invalid, it generates a form with error messages. The form includes fields for name, email, website, subject, and message. It also provides options for a spam-reduction question and a checkbox for users to receive a copy of the message.

Shortcode: [easy-contact]

Examples and Usage

Basic example – Display the contact form on your page or post with the default settings.

[easy-contact]

Advanced examples

Display the contact form on your page or post with custom attributes. The attributes allow you to customize the behavior of the contact form.

Example 1: Using the shortcode to display a contact form with a custom recipient and subject. The recipient email and subject line will be used when sending the email.

[easy-contact recipient="example@example.com" subject="Custom Subject"]

Example 2: Using the shortcode to display a contact form with a custom success message. The success message will be displayed to the user after they submit the form.

[easy-contact success_message="Thank you for contacting us! We will get back to you soon."]

Please note that in the advanced examples, you need to replace the attribute values with your own. For instance, replace “example@example.com” with the actual email address where you want to receive the contact form submissions.

PHP Function Code

In case you have difficulties debugging what causing issues with [easy-contact] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'easy-contact', 'ec_shortcode' );

Shortcode PHP function:

function ec_shortcode($attr) {
	global $ec_form_fields;
	// If we're processing $POST data without a problem, let's send an email
	if ( ec_check_input() ) {
		// Let's get some variables we're going to use multiple times
		$cc_option     =  get_option('ec_option_cc');
		$recipient     =  attribute_escape(get_option('ec_recipient_name')) . ' <' . attribute_escape(get_option('ec_recipient_email')) . '>';
		$user          =  strip_tags(trim($_POST['ec_name'])) . ' <' . strip_tags(trim($_POST['ec_email'])) . '>';
		$orig_referer  =  strip_tags(trim($_POST['ec_orig_referer']));
		$keywords      =  ec_get_query($orig_referer);
		// Start our email with its headers
		$headers       =  "MIME-Version: 1.0\r\n";
		// Our form has to match the encoding the user is typing it in, i.e., your blog charset
		$headers      .=  'Content-Type: text/plain; charset="' . get_option('blog_charset') . "\"\r\n";
		// Our generic mailer-daemon is just going to be WordPress@EXAMPLE.COM, where EXAMPLE.COM is your domain in lowercase
		$sitename      =  strtolower($_SERVER['SERVER_NAME']);
		// If we have the www., let's drop it safely
		if ( substr( $sitename, 0, 4 ) == 'www.' ) {
			$sitename  =  substr( $sitename, 4 );
		}
		// Our from email address
		$from_email    =  apply_filters( "wp_mail_from", "wordpress@$sitename" );
		// Our from email name
		$from_name     =  apply_filters( 'wp_mail_from_name', 'WordPress' );
		// And we begin the headers
		$headers      .=  "From: $from_name <$from_email>\r\n";
		$headers      .=  "Reply-To: $user\r\n";
		// Since we allow CC'ing, we can smartly only send one email
		if ( $cc_option && $_POST['ec_option_cc'] ) {
			// If CC'ing, we'll BCC: you and TO: the user.
			$to        =  $user;
			$headers  .=  "Bcc: $recipient\r\n";
		} else {
			// If no, just TO: you.
			$to        =  $recipient;
		}
		// We should include X data for the mailer version
		$headers      .=  'X-Mailer: PHP/' . phpversion() . "\r\n";
		// Build our subject line fo the email
		$subject       =  attribute_escape(get_option('ec_subject')) . ' ' . $_POST['ec_subject'];
		// And our actual message with extra stuff
		$message       =  strip_tags(trim($_POST['ec_message'])) . "\n\n---\n";
		$message      .=  __( 'Website: ', 'easy_contact' ) . strip_tags(trim($_POST['ec_url'])) . "\n\n---\n";
		$message      .=  __( 'IP address: ', 'easy_contact' ) . 'http://ws.arin.net/whois/?queryinput=' . ec_get_ip() . "\n";
		// Don't show keywords in the email unless we have some
		if ($keywords) {
			$message  .=  __( 'Keywords: ', 'easy_contact' ) . $keywords . "\n";
		}
		$message      .=  __( 'Form referrer: ', 'easy_contact' ) . strip_tags(trim($_POST['ec_referer'])) . "\n";
		$message      .=  __( 'Orig. referrer: ', 'easy_contact' ) . $orig_referer . "\n";
		$message      .=  __( 'User agent: ', 'easy_contact' ) . trim($_SERVER['HTTP_USER_AGENT']) . "\n";
		// Let's build our email and send it
		mail( $to, $subject, $message, $headers );
		// And then build the response message output for the user
		$output = "\n" . '<div class="formcontainer">' . "\n\t" . stripslashes(get_option('ec_msg_success')) . "\n</div>";
		// Returns the success message to the user
		return $output;
	// Otherwise, let's build us a form
	} else {
		// We begin our form.
		$form = '<div class="formcontainer">';
		// If we have an error, display it
		if ( $ec_form_fields['error'] != null ) {
			// Display an error message first. (We're applying our own easy_contact_text filter to prettify the message.)
			$form .= "\n\t" . apply_filters( 'easy_contact_text', stripslashes($ec_form_fields['error']) );
		} else {
			// Otherwise, display the intro message. (Also, filter this too.)
			$form .= "\n\t" . apply_filters( 'easy_contact_text', stripslashes(get_option('ec_msg_intro')) );
		}
		// Gather variables for multiple uses
		$required = stripslashes(get_option('ec_text_required'));
		$option_verf = get_option('ec_option_verf');
		// And continuing with our form
		$form .= '
	<form class="contact-form" action="' . get_permalink() . '" method="post">
		<fieldset>
			<legend>' . attribute_escape(get_option('ec_field_info')) . '</legend>
			<div class="form-label"><label for="ec_name">' . stripslashes(wp_filter_post_kses(get_option('ec_label_name'))) . ' ' . $required . '</label></div>
			<div class="form-input">' . $ec_form_fields['name'] . '</div>
			<div class="form-label"><label for="ec_email">' . stripslashes(wp_filter_post_kses(get_option('ec_label_email'))) . ' ' . $required . '</label></div>
			<div class="form-input">' . $ec_form_fields['email'] . '</div>
			<div class="form-label"><label for="ec_url">' . stripslashes(wp_filter_post_kses(get_option('ec_label_website'))) . '</label></div>
			<div class="form-input">' . $ec_form_fields['url'] . '</div>
		</fieldset>
		<fieldset>
			<legend>' . attribute_escape(get_option('ec_field_message')) . '</legend>
			<div class="form-label"><label for="ec_subject">' . stripslashes(wp_filter_post_kses(get_option('ec_label_subject'))) . ' ' . $required . '</label></div>
			<div class="form-input">' . $ec_form_fields['subject'] . '</div>
			<div class="form-label"><label for="ec_message">' . stripslashes(wp_filter_post_kses(get_option('ec_label_message'))) . ' ' . $required . '</label></div>
			<div class="form-textarea">' . $ec_form_fields['message'] . '</div>
		</fieldset>
		<fieldset>
			<legend>' . attribute_escape(get_option('ec_field_confirm')) . '</legend>';
		// If employing a spam-reduction question, insert it
		if ( $option_verf > 1 ) {
			if ( $option_verf == 2 || $option_verf == 4 ) {
				$form .= '
			<div class="form-label"><label for="ec_challenge_a">' . apply_filters( 'easy_contact_text', stripslashes(wp_filter_post_kses(get_option('ec_challenge_q'))) ) . ' ' . $required . '</label></div>
			<div class="form-input">' . $ec_form_fields['challenge_a'] . '</div>';
			}
			if ( $option_verf == 3 || $option_verf == 4 ) {
				// Big number is between 1 and 1,000.
				$big = rand( 1, 1000 );
				// Small number is between 1 and 10.
				$small = rand( 1, 10 );
				// We need to remember this session to validate the answer
				$_SESSION['check_math'] = $big+$small;
				$form .= '
			<div class="form-label"><label for="ec_math_a">' . __( "What is the sum of $big and $small?", "easy_contact" ) . ' ' . $required . '</label></div>
			<div class="form-input">' . $ec_form_fields['math_a'] . '</div>';
			}
		}
		// If the user can CC themselves, give them a box to tick
		if ( get_option('ec_option_cc') == true ) {
			$form .= '
			<div class="form-option">' . $ec_form_fields['option_cc'] . ' <label for="ec_option_cc">' . apply_filters( 'easy_contact_text', stripslashes(wp_filter_post_kses(get_option('ec_label_cc'))) ) . '</label></div>';
		}
		// Let's finish up with this form.
		$form .= '
			<div class="form-submit">
				<input type="submit" name="submit" class="button" value="' . attribute_escape(get_option('ec_text_submit')) . '" />
				<input type="hidden" name="ec_stage" value="process" />
				<input type="hidden" name="ec_referer" value="' . wp_specialchars( $_SERVER['HTTP_REFERER'], 1 ) . '" />
				<input type="hidden" name="ec_orig_referer" value="' . wp_specialchars( $_SESSION['orig_referer'], 1 ) . '" />
			</div>
		</fieldset>
	</form>
</div>';
		// The output is the form.
		$output = $form;
		// So output it.
		return $output;
	}
}

Code file location:

easy-contact/easy-contact/econtact.php

Conclusion

Now that you’ve learned how to embed the Easy Contact Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *