Jetpack by WordPress.com
[resolved] Jetpack contact form not working with do_shortcode() (9 posts)

  1. Chris
    Posted 2 years ago #

    I have encountered the same problem that was raised but not addressed in the thread, Jetpack Contact From not Sending when in do_shortcode.

    Without success, I also tried the solution offered here: Resolved: Jetpack contact form submitting to weird places.

    In the most basic terms, the Jetpack contact form works fine when I insert the shortcode into a post's or page's content via the UI text editor. However, if I want to use the Jetpack contact form shortcode in my template files using do_shortcode(), the form appears on the page but no email is sent and no message displays when submitting the form.

    Looking at the contact module code, I happened upon this:

    $form = Grunion_Contact_Form::$last;
    if ( !$form || ( is_wp_error( $form->errors ) && $form->errors->get_error_codes() ) ) {
    // Process the form

    $form is for some reason returning false whenever I use do_shortcode(), so process_submission() never runs. I have tested the same shortcode with do_shortcode() and in the default text editor, so I was able to verify that the shortcode works in the editor but not in do_shortcode(). Does anyone have thoughts on how to solve this problem?


  2. Jeremy Herve
    Jetpack Mechanic
    Plugin Author

    Posted 2 years ago #

    You won't be able to insert forms with do_shortcode, but you can use the following work-around to add Contact forms anywhere in your templates:

    1. Create a new Page, and insert a Contact form in the page.
    2. Note that Page's ID
    3. In your template, add the following code:
      	query_posts( array( 'page_id' => 123 ) ); // ID of the page including the form
      	if ( have_posts() ) : while ( have_posts() ) : the_post();
      	endwhile; endif;
  3. Chris
    Posted 2 years ago #


    Thanks, I have successfully implemented this solution. While testing it I found that the default subject line of the email is now the name of the page where I placed the contact form. Unfortunately I need the subject to remain equal to the post title where this contact form will be displayed; is there a way to do this?

    Previous to updating Jetpack and WordPress to their newest versions, I was using do_shortcode() without any problems. Are there plans to make this usability available again?


  4. Chris
    Posted 2 years ago #

    I was able to implement a solution for forcing the subject line to be what I want it to be (in this case, the title of the post on whose template file I am placing the contact form).

    First, hook the custom subject function into the Grunion Contact Form filter:

    add_filter('contact_form_subject', 'product_contact_subject');

    Now, the function:

    function post_contact_subject($subject) {
    	$url = $_SERVER['HTTP_REFERER'];
    	//Create an array divided by the base url. This example assumes that the post slug immediately follows the root domain. Change this to match your permalink structure.
    	$arr = explode(get_site_url() . '/', $url);
    	//Make sure that the array has at least two elements.
    	if (count($arr) < 2)
    		return $subject;
    	//Get the slug and remove the query string and any trailing slash if it exists.
    	$slug = str_replace('/', '', $arr[1]);
    	$slug = preg_replace('/\?.*/', '', $slug);
    	//Get the post id based on the slug.
    	$id = slug_to_id($slug);
    	$title = get_the_title($id);
    	//Return the custom title
    	return $title;

    Now, the function for getting a post's ID from its slug:

    function slug_to_id($slug, $type = 'post') {
    	  'name' => $slug,
    	  'post_type' => $type,
    	  'post_status' => 'publish',
    	  'showposts' => 1,
    	  'caller_get_posts'=> 1
    	$my_posts = get_posts($args);
    	if( $my_posts ) {
    		return $my_posts[0]->ID;
    	} else {
    		return 0;

    I hope that this helps anybody else with this problem.

  5. TJ Kelly
    Posted 2 years ago #

    Hi Jeremy and Comrade, I have this problem too. I followed Jeremy's advice and used a page with a Jetpack form on it. The form still doesn't seem to be functioning--neither email nor Feedback.

    I have another Jetpack form on a different page which saves in Feedback just fine (no email sent, but I think that's a server problem).

    Here are the pages in question:

  6. WPyogi
    Forum Moderator
    Posted 2 years ago #

    @TJ Kelly - this thread is outdated and already marked resolved. Please start your own thread per the forum guidelines -


    You can do so here:


  7. TJ Kelly
    Posted 2 years ago #

    Sorry, I didn't notice the resolved status. Thank you

  8. Ian Dunn
    Plugin Contributor

    Posted 1 year ago #

    There's a patch for this now at https://github.com/Automattic/jetpack/issues/102

  9. Jeremy Herve
    Jetpack Mechanic
    Plugin Author

    Posted 1 year ago #

    The problem was fixed in this commit. We'll include the fix in the next Jetpack release.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Jetpack by WordPress.com
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic