admin管理员组文章数量:1131461
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>
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>
Share
Improve this question
edited Nov 20, 2020 at 10:10
Davood Kazemi
asked Nov 18, 2020 at 21:13
Davood KazemiDavood Kazemi
31 silver badge6 bronze badges
1
- I made a complete revising. Wrote shorter codes and double-checked it before pasting it here. – Davood Kazemi Commented Nov 20, 2020 at 9:59
1 Answer
Reset to default 0Updated
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 );
}
本文标签: phpAdding featured image to a new post using frontend form
版权声明:本文标题:php - Adding featured image to a new post using front-end form? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736768929a1951975.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论