[英]How to save data in a custom post type in wordpress before sending the email?
I have a problem in Wordpress. 我在Wordpress中遇到了问题。 I want to make a hook on Contact Form 7, so that when the user clicks send, it first saves this information inside a custom post type.
我想在联系表单7上创建一个钩子,这样当用户单击发送时,它首先将此信息保存在自定义帖子类型中。
Reading the documentation, I found this 阅读文档,我发现了这一点
// run the action do_action( 'wpcf7_before_send_mail', $contact_form ); //运行动作do_action('wpcf7_before_send_mail',$ contact_form);
// define the wpcf7_before_send_mail callback
function action_wpcf7_before_send_mail( $contact_form ) {
//code
};
// add the action
add_action( 'wpcf7_before_send_mail', 'action_wpcf7_before_send_mail', 10, 1 );
But I do not know how to continue. 但我不知道如何继续。 Has anyone done this and can you help me please?
有没有人这样做,你能帮我吗?
function action_wpcf7_before_send_mail( $contact_form ) {
$post_content = ''; // empty contebt
foreach ($_REQUEST as $key => $value) {
$post_content .= $key.': '.$value.'
'; //add each form field to content
}
$title = $_REQUEST['some field'].' '.$_REQUEST['some field2']; // generate dynamic title
$t = time();
$thash = md5($t);
$my_query = array(
'post_title' => wp_strip_all_tags( $title ),
'post_content' => $post_content,
'post_type' => 'your-post-type',
'post_name' => $thash,
'post_status' => 'publish',
'post_author' => 1
);
$data = wp_insert_post( $my_query );
return $contact_form;
};
// add the action
add_action( 'wpcf7_before_send_mail', 'action_wpcf7_before_send_mail', 10, 1 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.