How to Automatically Insert Email Subscription in WordPress Posts

Photo of author

By Victor Ashiedu

Published

Do you want to insert your email subscription form into WordPress posts automatically? Doing this increases your email subscription rate, and this post teaches you how to do that.

Step 1: Install the Code Snippets Plugin

As I mentioned in the introduction, the recommended way to add codes to functions.php is via the Code Snippets Plugin. If you have not installed this plugin before, follow the steps below to install the plugin.

On the other hand, if you have already installed the plugin, feel free to jump to the next section of this article.

  1. Sign in to your WordPress dashboard with your admin account. Then, hover over Plugins and select Add New.
  1. When the Add Plugins page opens, enter Code Snippets in the search box (top right). Then, click Install Now to the top right of the Code Snippets plugin.

    The plugin will install. After it installs, click Activate. Then, proceed to the next step of this guide.
In the screenshot below, my Code Snippets plugin is not displaying Install Now because I already installed the plugin.

Step 2: Add a New Code in Code Snippets

As I mentioned in the introduction, you need to automatically add a code to insert email subscriptions in WordPress posts. Follow the steps below to add the custom code with the Code Snippets plugin.

  1. Sign in to your WordPress dashboard with your admin account. Then, hover over Snippets and select Add New.
How To Automatically Insert Email Subscription In WordPress Posts - step 2 - Add A New Code In Code Snippets
  1. When the Add New Snippet window opens, give the snippet a name (1) – see the screenshot beneath the code block below.

    Then, copy the code below to the code area (2). Finally, click Activate to save and activate the code on the top right of the window.
My code below adds my email subscription after the 7th paragraph. If you want to add yours after a different paragraph, change 7 (highlighted in my screenshot below). Here is the line of code that contains the number 7

return prefix_insert_after_paragraph( $email_subscription_code, 7, $content );

Finally, before you move on, copy the code for your email subscription form to the part of the code I boldened – add code here.
//Insert email subscription form after 7th paragraph
add_filter( 'the_content', 'prefix_insert_post_email_subscription' );
function prefix_insert_post_email_subscription( $content ) {
    $email_subscription_code = '<center>add code here </br></center>';
    if ( is_single() && !is_admin() ) {
        return prefix_insert_after_paragraph( $email_subscription_code, 7, $content );
 }
    return $content;
}

// Parent Function
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );
    foreach ($paragraphs as $index => $paragraph) {
        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }
        if ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }
    }
    return implode( '', $paragraphs );
	
}

Conclusion

I always wanted to insert my email subscription form within my WordPress posts automatically. However, I did not know how to do it.

When I eventually found out how to do this, my email subscription skyrocketed! Based on the level of success I had with it, I decided to share how I did it.

So, there you have it – the steps to insert an email subscription form in WordPress posts automatically! I hope you found the guide easy to follow.

I also hope that you’ve successfully inserted your email subscription form into your articles. If you did, click on “Yes” beside the “Was this page helpful” question below.

You may also express your thoughts and opinions by using the “Leave a Comment” form at the bottom of this page.

Finally, visit our WordPress & Websites How-Tos page for more WordPress and general website guides.

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

We're committed to writing accurate content that informs and educates. To learn more, read our Content Writing Policy, Content Review Policy, Anti-plagiarism Policy, and About Us.

However, if this content does not meet your expectations, kindly reach out to us through one of the following means:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

Leave a comment