Add form to all posts automatically

Adding a MailChimp for WordPress sign-up form to the end of all singular posts is easy. Just add the following snippet your theme its functions.php file, for example.

You can find this file in the following location: `/wp-content/themes/your-theme/functions.php`

Please make sure to change the `$form_id` variable to the actual ID of the form you would like to show. If you omit this line, your default form will be showed.

/**
 * Adds a form to the end of all single posts
 * 
 * @param string $content
 * 
 * @return string $content
 */
function myprefix_add_form_to_posts( $content ) {
    
    // Change to ID of the form you want to add
    $form_id = 0;
    
    // Check if this is a single post. 
    if ( is_singular( 'post' ) ) {
        
        // Add the form to the end of the post content.
        $content .= mc4wp_get_form( $form_id );
        
    }
    
    // Returns the content.
    return $content;
}
 
add_filter( 'the_content', 'myprefix_add_form_to_posts' );

Was this article helpful? ·