简体   繁体   English

如何在发送电子邮件之前在wordpress中的自定义帖子类型中保存数据?

[英]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.

相关问题 Wordpress-如何在保存/更新之前检查自定义帖子类型标题是否唯一? - Wordpress - How to check if a custom post type title is unique before save/update? 如何在WordPress自定义帖子类型中保存多个复选框 - How to save multiple checkboxes in WordPress custom post type 如何使 Wordpress save_post_($post-type) 挂钩适用于多种自定义帖子类型 - How to make Wordpress save_post_($post-type) hook works with multiple Custom Post Types 如何将CSV / Excel数据导入Wordpress自定义帖子类型? - How to import CSV / Excel data into wordpress custom post type? 如何在点击时使用jQuery动态加载WordPress自定义帖子类型数据? - How to dynamically load WordPress custom post type data with jQuery on click? WordPress-将元数据分配给自定义帖子类型 - WordPress - Assign Meta Data to Custom Post Type 发送到URL之前如何保存表单数据 - How to save form data before sending to URL 创建自定义帖子类型时,WordPress将电子邮件发送给所选用户 - Wordpress send email to selected user when custom post type is created 新的自定义帖子类型加载之前的WordPress钩子 - Wordpress hook before new custom post type load WordPress自定义帖子类型保存单选输入元 - Wordpress custom post type save radio input meta
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM