WordPress Development Asked by Davood Kazemi on January 3, 2021
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:
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;
}
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>
<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>
Updated
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 );
}
Answered by hrsetyono on January 3, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP