Contact Form 7 is the easiest, free and mostly used plugin for creating forms in WordPress. It is also highly customizable. We can send emails in a customized manner or layout. Most of the times we use the form fields to reflect in the mail. So far this is easily doable.

But have you ever thought about showing any other possible information in the mail? Let’s say, you want to show the IP address where the contact details were sent from.

Well, this is already available with Contact Form 7 and that is one of the Special Mail Tags. Here the tag we will be using is [_remote_ip]. You can have a complete list of existing special tags here.

But we will not stop here rather we will go one step further. Let’s say is there any possibility of having more such tags? The good news is YES. Contact Form 7 provides hook for this and we can use that. Let’s have a functionality here for using site url as a special tag.

// Hook for additional special mail tag
add_filter( 'wpcf7_special_mail_tags', 'wti_special_mail_tag', 20, 3 );

/**
 * Special mail tag for site url
 *
 * @param string
 * @param string
 * @param string
 *
 * @return string
 */
function wti_special_mail_tag( $output, $name, $html )
{
	// For backwards compatibility
	$name = preg_replace( '/^wpcf7\./', '_', $name );

	if ( '_site_url' == $name ) {
		// Get the site url
		$blog_url = get_option( 'siteurl' );

                // Create clickable link
                $output = make_clickable( $blog_url );
	}

	return $output;
}

To use this in the mail template, you need to put [_site_url]. I have defined only one tag, you can add as many here based on conditions. Put the above code in a plugin or in the functions.php file of the active theme. That’s it. Happy Coding 🙂

2 thoughts on “Custom Contact Form 7 Special Mail Tags

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.