Adding The Post Title To My "Reply By Email" Button

15 Aug 2021 | ~4 minute read

I recently wrote about online conversations and engagements where I talked about using plain old email instead of comments. Off the back of that post, I got chatting with Luke Harris (told you that button works), who does something very similar on his site.

However, his reply button automatically adds the title of the post to the subject line of the email. Luke uses Eleventy for his blog, so if you want similar functionality on your Eleventy blog, you can read about how he did this.

Not wanting to be outdone by Luke, I decided to add similar functionality to my "Reply by email" button. But I'm not using Eleventy; I'm on good old WordPress. Lucky for me, with a bit of PHP trickery, I was able to do this relatively easily.

Adding PHP

First we need to add some PHP code to the functions.php file which will create a very simple shortcode.

From there we will call that shortcode in the HTML that the reply button uses. Simple, hey?

Side note: if you're using a theme that someone else has created, I would strongly suggest you setup a child theme first. Otherwise, any changes you make will be overwritten when the developer updates the theme.

Ok let’s crack on. First you need to add the following code to the end of your functions.php file:

// Add shortcode for reply via mail link
add_shortcode( 'mailto_title', 'mailto_title' );

function mailto_title( $atts ) {
    return esc_attr( get_the_title( get_the_ID() ) );
}

All this shortcode does is parse the title of the post and embed that where the shortcode is used. Pretty darn simple.

Adding the reply button

That's it for the PHP. All we need to do now is add our shiny new shortcode to the mailto link we will use on our reply button.

To do that, we need to add a button somewhere at the end of a post. There are various plugins that can do this, or you can do it yourself by adding the appropriate HTML to your single.php file.

Personally, I'm using the awesome GeneratePress theme, so can easily add a button after all my posts using Generate Blocks.

Now, if you wanted to add a standard link that sends you an email, the HTML markup would look something like this:

<a href=“mailto:you@example.com”>Reply by email</a>

But that won't populate the subject for us. To do that, we need to add a little more HTML to the link:

<a href=“mailto:you@example.com?subject=Reply to your post”>Reply by email</a>

And when we click on that link, we get this result:

Woohoo! We have a standard link that will populate the email address and subject, but it isn’t a button and it isn’t using the post’s title. To do this, create a WordPress button in the Gutenberg editor and add the following string to the link value:

mailto:you@example.com?subject=Reply to "[mailto_title]"

Then, go to the front end and test your button. The result should be something like this:

Well would you look at that, folks! We not only have the subject populated, but it's using the title of the post too. Goddam, WordPress is great, isn't it.

If you want to do it the manual way, by adding the HTML to your single.php file, then add the following HTML remembering to change the CSS class to whatever the right one is for buttons in your theme:

<a class=“button” href=“mailto:you@example.com?subject=<?php echo do_shortcode( ‘[mailto_title]’ ); ?>”>Reply by email</a>

Closing thoughts

So there you go, folks. If you're using WordPress you can now add a “Reply to my post” button and it will automatically populate the subject line of the email with the post title.

It's a simple thing, but it makes it really easy for you, as the person who is receiving the email, to understand the context of what your reader is emailing you about.

Before implementing this change, I received lots of emails with a generic subject line like "saw your post..." This makes it difficult to understand what post they're talking about sometimes, so adding this little tweak really helped.

Are you using a "reply to this post" button? Or are you still rocking comments? Why not use the button below to tell me...and yes, it does add the post title to the subject line. 🙃

← The one before
Story Time

Up next →
Moving From Yoast To SEOPress

Get in touch!

Receiving emails from my readers is my favourite thing, so if you have something to say, feel free to drop me an email or sign my guestbook.

Want more content?

Say no more, dear reader. Here's three random posts from this blog for you to peruse:

Why I Don’t Use A Static Site Generator
08 Apr 2020

A Case for Toe Socks
02 Jul 2023

How To Install Nextcloud On Shared Hosting
16 Jun 2019

Want to be informed when I post new articles? Simply enter your email address below and you will get an email whenever new posts are published.

Alternatively, you can subscribe via RSS instead.

Enjoyed this post?

I put a lot of work into maintaining this site and I really enjoy interacting with my readers.

My fuel of choice is coffee, so if you did enjoy this post, or found it in any way useful, I'd appreciate more fuel to keep me going. ❤️

Buy me a coffee