Add Current Post Title and Link to Contact Form Emails

|

Divi includes a contact form module which can lets your users send messages to your email inbox through your site. If you have contact forms on multiple posts / pages, it can be useful to know which post / page the user submitted the contact form on. Here's how to add the post title / url to the contact form module emails.

First, add the following PHP code to your site:

add_filter('et_pb_module_shortcode_attributes', 'dbc_add_post_link_to_contact_form', 10, 3);

function dbc_add_post_link_to_contact_form($props, $attrs, $render_slug) {
	if ($render_slug !== 'et_pb_contact_form' || !is_array($props)) { return $props; }
	if (!empty($props['custom_message'])) {
		$title = get_the_title();
		$url = get_permalink();
		$props['custom_message'] = str_replace('%%post_name%%', $title, $props['custom_message']);
		$props['custom_message'] = str_replace('%%post_url%%', $url, $props['custom_message']);
		$props['custom_message'] = str_replace('%%post_link%%', '<a href="'.esc_attr($url).'" data-et-target-link="_blank">'.esc_html($title).'</a>', $props['custom_message']);
	}
	return $props;
}
That adds the following placeholders for use in the contact form module:

%%post_name%% – the title of the post / page
%%post_url%% – the URL of the post / page
%% post_link%% – a html link containing the post title and URL

You can now use them in the contact form custom message pattern field, e.g:

This post may contain referral links which may earn a commission for this site

Divi Booster

Hundreds of new features for Divi
in one easy-to-use plugin

0 Comments

Submit a Comment

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