Have a front-end form to create a post, Was only able to add text data to the new post (title, content, text-typed custom fields). I want to add a featured image to the new posts too.

It's a WordPress site and I use Avada theme. The custom post type is the Avada's default Portfolio post. But generalised answers would be very helpful too (please give some explanation on your code).

Please add some code to my PHP code and the form so that it can save a featured image too.

So, Here's my php code in fuctions.php in my child-theme:


    $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 !';

My front-end form:

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

<button type="submit">Submit</button>
  • I made a complete revising. Wrote shorter codes and double-checked it before pasting it here. Nov 20, 2020 at 9:59

1 Answer 1



I'm not sure how to do it without ContactForm 7 plugin, but here's my past code on how to do it in CF7:

Assuming your file field is [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 );

  • 1
    I'd be glad if you give some explanation. I am very new to PHP and WordPress. Does it work on my code? I will try by I think I need explanation. Nov 20, 2020 at 8:46
  • I used it in every way I could and it didn't work. Where exactly should I place this PHP code? CF7 code for addind image file: [file my_cf7_save_featured_image] The submit button: [submit "wpcf7_submit"] Nov 20, 2020 at 9:38
  • Updated my answer. I thought your existing code already does the uploading. I copied my code from past project. There might be some typo in field name, but you can adapt from there.
    – hrsetyono
    Nov 20, 2020 at 13:54

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .