是否使用前端表单将特色图片添加到新帖子?

时间:2020-11-18 作者:Davood Kazemi

有一个前端表单来创建帖子,只能向新帖子添加文本数据(标题、内容、文本类型自定义字段)。我想在新帖子中添加一个特色图片。

这是一个WordPress网站,我使用Avada主题。自定义帖子类型是Avada的默认类型Portfolio发布。但概括的答案也会很有帮助(请对您的代码进行一些解释)。

请在我的PHP代码和表单中添加一些代码,这样它也可以保存一个特色图像。

下面是我的php函数代码。我的孩子主题中的php:

    if(isset($_POST[\'title\'])){

    $custom_field_address1 = $_POST[\'address1\'];

    $my_post = array(

    \'post_title\' => $_POST[\'title\'],
    \'post_content\' => $_POST[\'description\'],
        
    \'post_status\' => \'publish\', 
    \'post_type\' => \'your_post_type_name\',
    \'meta_input\' => array(
        \'address1\' => $custom_field_address1,
        )
    );
    
    $post_id = wp_insert_post($my_post);

    add_post_meta( $post_id, \'address1\', $custom_field_address1, false );
    echo \'New Post Saved !\';
    
    die;
    }
我的前端表单:

<form method="post">
<div class="form-group">
      <label for="title">Post Title:</label>
      <input type="text" class="form-control" id="title" name="title">
</div>
    
    
<div class="form-group">
      <label for="pwd">Post Description :</label>
      <textarea class="form-control"  name="description"></textarea>
</div>
      
<div class="form-group">
      <label for="address1">Address :</label>
      <input type="text" name="address1" id="address1">
</div>

<BR>
<button type="submit">Submit</button>
</form>

1 个回复
SO网友:hrsetyono

Updated

我不知道没有ContactForm 7插件怎么做,但下面是我过去在CF7中的代码:

假设您的文件字段为[file your-file].

add_action( \'wpcf7_before_send_mail\', \'my_cf7_save_featured_image\', 10, 2 ); 

function my_cf7_save_featured_image( $instance, $result ) {
  require_once( ABSPATH . \'wp-admin/includes/image.php\' );
  require_once( ABSPATH . \'wp-admin/includes/admin.php\' );  

  // Get submission data instead of using $_POST
  $sub = \\WPCF7_Submission::get_instance();
  $data = $sub->get_posted_data();

  // Create post
  $my_post = array(
    \'post_title\' => $data[\'title\'],
    \'post_content\' => $data[\'description\'],
    \'post_status\' => \'publish\', 
    \'post_type\' => \'post\',
  );
    
  $post_id = wp_insert_post($my_post);
  add_post_meta( $post_id, \'address1\', $data[\'address1\'], false );
  
  // extract submitted file
  $uploaded_files = $sub->uploaded_files();
  $image_name = $data[\'your-file\'];
  $image_location = $uploaded_files[\'your-file\'];
  $image_content = file_get_contents( $image_location );

  // set upload path
  $dir = wp_upload_dir();
  $upload = wp_upload_bits( $image_name, null, $image_content );
  $filename = $upload[\'file\'];
  
  $wp_filetype = wp_check_filetype( basename( $filename ), null );

  $attachment = array(
        \'post_mime_type\' => $wp_filetype[\'type\'],
        \'post_title\' => preg_replace( \'/\\.[^.]+$/\', \'\', basename( $filename ) ),
        \'post_content\' => \'\',
        \'post_status\' => \'inherit\',
  );

  // start upload and attach to post
  $attach_id = wp_insert_attachment( $attachment, wp_slash( $filename ), $post_id );

  // update image metadata
  $attach_data = wp_generate_attachment_metadata( $attach_id, wp_slash( $filename ) );
  wp_update_attachment_metadata( $attach_id, $attach_data );
}