【问题标题】:How to save data in a custom post type in wordpress before sending the email?如何在发送电子邮件之前将数据保存在 wordpress 中的自定义帖子类型中?
【发布时间】:2019-06-07 22:52:16
【问题描述】:

我在 Wordpress 中遇到问题。我想在 Contact Form 7 上做一个钩子,这样当用户点击发送时,它首先将此信息保存在自定义帖子类型中。

阅读文档,我发现了这个

// 运行动作 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 ); 

但我不知道如何继续。有没有人这样做过,你能帮帮我吗?

【问题讨论】:

    标签: php wordpress hook contact-form-7


    【解决方案1】:
    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 ); 
    

    【讨论】:

      猜你喜欢
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 1970-01-01
      • 2017-01-03
      • 2022-01-06
      • 1970-01-01
      • 2013-04-24
      相关资源
      最近更新 更多