admin管理员组

文章数量:1134570

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?

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?

Share Improve this question asked Jun 7, 2023 at 9:19 Hasut92Hasut92 11 bronze badge 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 Commented Aug 11, 2023 at 14:09
Add a comment  | 

2 Answers 2

Reset to default 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);
}

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;
}

本文标签: Adding filter to the Contact Form 7 response