Adding featured image to a new post using front-end form?


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. So, I want the code to work on that post type. (Couldn’t use plugins like Post My CF7 Form. They didn’t work on my website. Need to write 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:

///How to submit custom post type in wordpress from the Front-End


//echo $_POST['title']; // print title variable value

// create post object

$custom_field_add1 = $_POST['add1'];
$custom_field_phone = $_POST['phone'];

$my_post = array(

'post_title' => $_POST['title'],
'post_content' => $_POST['description'],
'post_status' => 'publish', 
'post_type' => 'companies',
'meta_input' => array(
    'add1' => $custom_field_add1,
    'phone' => $custom_field_phone,

// i use wordpres predefine function/
$post_id = wp_insert_post($my_post);

add_post_meta( $post_id, 'add1', $custom_field_add1, false );
add_post_meta( $post_id, 'phone', $custom_field_phone, false );
echo 'New Post Saved !';

die; // stop script after form submit

My CF7 form:

<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">Description (Post content):</label>
      <textarea class="form-control"  name="description"></textarea>

      <div class="form-group">
      <label for="add1">Address:</label>
      <input type="text" name="add1" id="add1">
      <div class="form-group">
      <label for="phone">Phone:</label>
      <input type="number" name="phone" id="phone">

Davood Kazemi 2 months 0 Answers 19 views 0

Leave an answer