Add Current Post Title and Link to Divi Contact Form Emails

Written by Dan Mossop

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; 
	}
	
	// Don't apply when in visual builder
	if (is_admin() || function_exists('et_core_is_fb_enabled') && et_core_is_fb_enabled()) {
		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).'" target="_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:

Run PHP Code Directly in your Divi Layouts

Unlock endless customization, automation, and dynamic functionality by seamlessly adding PHP code to your Divi pages and posts with the Divi PHP Code Module. Style, preview, and debug your PHP creations directly in the visual builder with robust error handling and enhanced security.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

7 Comments

  1. i works but the strange things is after adding:

    %%post_name%%
    %%post_url%%

    then saving the page, the variables are replaced with the actual content?

    is there no way to add the variables and let them remain there as variables so that they only add the post or page name and url at the time of creating the email ? this would allow for easy copy of the module to multiple pages

    Reply
    • Hey Bijal, thanks for pointing this out. I've updated the code to prevent it from running when editing the page, which should prevent the placeholders from being replaced with the actual content in the contact form module settings. I hope it helps, but let me know if not. Thanks!

      Reply
      • Thanks Gustavo, will check this out today and let you know how it went. this really is a great little snippet.

        and thank you for making the effort and taking the time.

        Reply
      • oops sorry Dan (not gustavo)… had my brain freezing up a bit there… and I checked it out and it works great.

        thank you, much appreciated.

        Reply
        • Ha ha, no worries, Bijal! I'm glad the update is working – thanks for confirming!

          Reply
  2. Thank you very much!! This piece of code was totally helpful.

    Great job. It worked great for me. Appreciate it a lot.

    Reply
    • You're very welcome, Gustavo! I find this one very useful myself :)

      Thanks!

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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

We may earn a commission when you visit links on our website.

Latest Posts

Fade a Divi Image Module Edge into the Background

Want to create a stylish fade effect on your Divi image module—where one side fades smoothly into the background? With a bit of CSS, you can make any edge (or corner) of the image fade out: top, bottom, left, right, or even diagonally.Fade a Divi Image Module Edge...

Hide the Header and Footer in the Hello Elementor Theme

Removing the default header and footer from your Hello Elementor theme allows for a streamlined and distraction-free website design. This is especially useful when creating unique landing pages, full-width layouts, or custom headers and footers with a page builder. In...

Setting up the Divi Password Box Module

Setting up password protection on a page can help you control access to sensitive or private content in WordPress, allowing only authorized visitors to view certain sections. With the Divi Password Box module, you can replace the plain Divi password form with a fully...

Open Divi Social Media Icons in a New Tab

Ensuring your website's social media icons open in a new browser tab creates a seamless user experience and keeps visitors engaged with your site while allowing them to explore your social media presence. This approach prevents users from navigating away from your...

Set Hover Color for Menu Links in the Divi Menu Module

Customizing the hover color of menu links lets you enhance the user experience and maintain visual consistency with your website’s brand. By setting a specific hover color, you can guide visitors' attention and improve navigation feedback. In this guide we show you...

Random Posts