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?
- 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
2 Answers
Reset to default 1You 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
版权声明:本文标题:Adding filter to the Contact Form 7 response 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736843359a1955197.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论