联系表格7以自定义帖子类型

Kyl*_*ser 4 php forms contact-form-7 custom-post-type

我想将联系表单从联系表单7转换为自定义帖子类型。

目前,这是我所拥有的:

<?php 

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&        $_POST['action'] == "front_post") {

//store our post vars into variables for later use
//now would be a good time to run some basic error checking/validation
//to ensure that data for these values have been set
$title     = $_POST['title'];
$content   = $_POST['content'];
$Interest   = $_POST['Interest'];
$post_type = 'purchase';


//the array of arguements to be inserted with wp_insert_post
$new_post = array(
'post_title'    => $title,
'post_content'  => $content,
'tags_input'  => $tags,
'posted_data' => $Interest,
'post_status'   => 'publish',
'post_category' => array('0',$_POST['cat']),          
'post_type'     => $post_type 
);

//insert the the post into database by passing $new_post to wp_insert_post
//store our post ID in a variable $pid
//we now use $pid (post id) to help add out post meta data
$pid=wp_insert_post($new_post);

//we now use $pid (post id) to help add out post meta data
add_post_meta($pid, 'cust_key', $custom_field);


}
?>
Run Code Online (Sandbox Code Playgroud)

这是实际表格的链接:http : //stage.icardpromotions.com/create-purchase-order/

我需要能够将此表单的所有信息提取到自定义帖子类型“购买”中

如您所见,我目前正在输入post_content,post_title等。

我也尝试过通过输入名称“ Interest”从内容表单中提取内容,但是它不起作用。

有人知道如何执行此操作吗?

小智 6

 function save_posted_data( $posted_data ) {


       $args = array(
         'post_type' => 'post',
         'post_status'=>'draft',
         'post_title'=>$posted_data['your-name'],
          'post_content'=>$posted_data['your-message'],
       );
       $post_id = wp_insert_post($args);

       if(!is_wp_error($post_id)){
         if( isset($posted_data['your-name']) ){
           update_post_meta($post_id, 'your-name', $posted_data['your-name']);
         }
        // if( isset($posted_data['your-email']) ){
        //   update_post_meta($post_id, 'your-email', $posted_data['your-email']);
        // }
        // if( isset($posted_data['your-subject']) ){
        //   update_post_meta($post_id, 'your-subject', $posted_data['your-subject']);
        // }
         if( isset($posted_data['your-message']) ){
           update_post_meta($post_id, 'your-message', $posted_data['your-message']);
         }
      //and so on ...
      return $posted_data;
     }
 }

add_filter( 'wpcf7_posted_data', 'save_posted_data' );
Run Code Online (Sandbox Code Playgroud)

--------------------解释--------------------------

首先使用make函数,并向其中添加一个钩子wpcf7_posted_data

- -第一步 - -

function save_posted_data( $posted_data ) {

}
add_filter( 'wpcf7_posted_data', 'save_posted_data' );
Run Code Online (Sandbox Code Playgroud)

- -第二步 - -

现在您需要在帖子中添加一些参数,需要使用 wp_insert_post();

$args = array(
         'post_type' => 'post',
         'post_status'=>'draft',
         'post_title'=>$posted_data['your-name'],
          'post_content'=>$posted_data['your-message'],
       );
$post_id = wp_insert_post($args);
Run Code Online (Sandbox Code Playgroud)

-第三步-

检查填充的项目是否错误

if(!is_wp_error($post_id)){ //do ur stuffs }
Run Code Online (Sandbox Code Playgroud)

---第四步-

现在检查是否设置了该字段,并更新了metas,例如post

if( isset($posted_data['your-name']) ){
    update_post_meta($post_id, 'your-name', $posted_data['your-name']);
}
Run Code Online (Sandbox Code Playgroud)

最后返回值

return $posted_data;
Run Code Online (Sandbox Code Playgroud)

完整代码在上面。


Aur*_*ata 0

有一个插件可以执行此操作,Post My CF7 Form

该插件允许您将 CF7 表单及其字段映射到现有帖子类型或新的自定义帖子类型。

映射过程是使用交互式 UI 管理页面完成的,您可以选择将字段映射到帖子字段(标题、内容、摘录、slug、作者)以及帖子元字段。

此外,该插件还引入了一个save提交按钮,允许用户保存表单的草稿版本,这对于大型复杂表单尤其有用。

该插件可以自动加载分类术语select/checkbox/radio该插件可以自动在已映射到该分类的字段

该插件有多个钩子和过滤器来定制流程。