0

I have a Contact Form 7 that adds a new post when submitted. In the response text that displays after submitted, I would like to include a link to that post.

add_action( 'wpcf7_before_send_mail', function($contact_form, &$abort, $submission){ if ($contact_form->title == "Pledge"){ $name = $submission->get_posted_data("pledge-name"); $schoolid = $submission->get_posted_data("yes"); $id = wp_insert_post([ "post_type" => "pledge", "post_title" => "$school->name | $name", "post_status" => "draft", ]); } }, 10, 3); 

How can I insert the $id variable into the response text?

1
  • wrt to mapping a form submission to a post, you might want to take a look at the Post My CF7 Form extension plugin
    – Aurovrata
    CommentedAug 11, 2023 at 14:09

2 Answers 2

1

You need to filter the submission message after the CF7 plugin has set its response using the display message filter wpcf7_display_message. You an achieve this with an anonymous fuction hook,

add_action( 'wpcf7_before_send_mail', function ( $contact_form, &$abort, $submission ) { //do you post creation and get the $id. add_filter('wpcf7_display_message', function($msg, $status) use ($id){ switch($status){ case 'mail_sent_ok': //mail sent. $msg="Created post {$id}"; case 'mail_sent_ng'://mail failed to send. break; case '': //mail aborted. break; } return $msg; },10,2); } 
    0

    Please find below a potential solution. It checks the status of the $id for being a \WP_Error (indicating the creation of the post failed). If it's not a error, it'll produce a simple message to give output the $id.

    However, it should be noted that the code you've provided has an error and will likely produce a fatal error, because $school is undefined and you're accessing it like an Object. My code is based on your code and doesn't correct for this. So you'll need to apply this solution to your particular implementation and correct this.

    Also, I've added $abort = true; in the case of error, so the form submission will be aborted. You can keep that or remove it depending on your requirements.

    add_action( 'wpcf7_before_send_mail', function ( $contact_form, &$abort, $submission ) { if ( $contact_form->title == "Pledge" ) { $name = $submission->get_posted_data( "pledge-name" ); $schoolid = $submission->get_posted_data( "yes" ); $id = wp_insert_post( [ "post_type" => "pledge", "post_title" => "$school->name | $name", "post_status" => "draft", ] ); if ( is_wp_error( $id ) ) { $submission->set_response( 'There was an error creating the new post.' ); $abort = true; } else { $submission->set_response( sprintf( 'A new post has been created with the ID: %s.', $id ) ); } } }, 10, 3 ); return !$abort; } 
    3
    • This looks good but it doesn't seem to work. (The $school undefined thing is just not relevant, I removed some code when including it in this post) I can't find a lot of documentation for $submission->set_response
      – Hasut92
      CommentedJun 8, 2023 at 10:44
    • are you definitely including this code in a place that gets executed before the form submissions is handled? Where are you including it? And is the $contact_form->title == "Pledge" definitely correct? If that's never correct none of the code executes.
      – Paul G.
      CommentedJun 8, 2023 at 11:18
    • This won't work because the CF7 plugin will reset the submission message after the mail is sent successfully. You need to change the response only after the mail is sent (see my answer above)
      – Aurovrata
      CommentedAug 11, 2023 at 14:07

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.