I am trying post submit on front end. All works without thumbnail uploading and setting. Here is my html form.
<form method="post" action="">
<input type="text" name="title">
<textarea name="content"></textarea>
<input type="file" name="thumbnail">
<input type="submit" name="submit">
</form>
And here is my php code.
<?php
if (isset($_POST['submit'])) {
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$title = $_POST['title'];
$content = $_POST['content'];
# Set Post
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'pending',
'post_type' => 'post',
);
$post_id = wp_insert_post($new_post);
# Set Thumbnail
$args = array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => 'pending',
'post_parent' => $post_id
);
$attachments = get_posts($args);
foreach($attachments as $attachment){
$attachment_id = media_handle_upload('thumbnail', $attachment->ID);
}
if (!is_wp_error($attachment_id)) {
set_post_thumbnail($post_id, $attachment_id);
header("Location: " . $_SERVER['HTTP_REFERER'] . "/?files=uploaded");
}
}
?>
It creates post ok. But it not sets any thumbnail. I don't know where i am wrong.
I am trying post submit on front end. All works without thumbnail uploading and setting. Here is my html form.
<form method="post" action="">
<input type="text" name="title">
<textarea name="content"></textarea>
<input type="file" name="thumbnail">
<input type="submit" name="submit">
</form>
And here is my php code.
<?php
if (isset($_POST['submit'])) {
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$title = $_POST['title'];
$content = $_POST['content'];
# Set Post
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'pending',
'post_type' => 'post',
);
$post_id = wp_insert_post($new_post);
# Set Thumbnail
$args = array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => 'pending',
'post_parent' => $post_id
);
$attachments = get_posts($args);
foreach($attachments as $attachment){
$attachment_id = media_handle_upload('thumbnail', $attachment->ID);
}
if (!is_wp_error($attachment_id)) {
set_post_thumbnail($post_id, $attachment_id);
header("Location: " . $_SERVER['HTTP_REFERER'] . "/?files=uploaded");
}
}
?>
It creates post ok. But it not sets any thumbnail. I don't know where i am wrong.
Share Improve this question asked Feb 16, 2018 at 23:22 wpdevwpdev 5492 gold badges13 silver badges28 bronze badges 3- is the media uploaded? Shouldn't media_handle_upload used before the attachment get_posts? Not sure the attachment get_posts query is needed at all. – Stefano Tombolini Commented Feb 17, 2018 at 16:02
- No. Media not uploading. Do you have any suggestion? – wpdev Commented Feb 17, 2018 at 16:24
- Check the answer I proposed. – Stefano Tombolini Commented Feb 17, 2018 at 16:47
3 Answers
Reset to default 0In form, you need the code enctype="multipart/form-data"
<form method="post" action="" enctype="multipart/form-data">
You can use the exemple provided by the wp_insert_attachment() function codex page and the set_post_thumbnail() too :
$file = $_FILES['thumbnail']['tmp_name'];
/*
* $filename is the path to your uploaded file (for example as set in the $_FILE posted file array)
* $file is the name of the file
* first we need to upload the file into the wp upload folder.
*/
$upload_file = wp_upload_bits( $filename, null, @file_get_contents( $file ) );
if ( ! $upload_file['error'] ) {
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( $filename, null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', $filename ),
'post_content' => '',
'post_status' => 'inherit',
'post_parent' => $post_id
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $upload_file['file'], $post_id );
if ( ! is_wp_error( $attachment_id ) ) {
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $upload_file['file'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
}
}
There seem to be some unnecessary code. Try the following one:
$title = $_POST['title'];
$content = $_POST['content'];
# Set Post
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'pending',
'post_type' => 'post',
);
$post_id = wp_insert_post($new_post);
# Set Thumbnail
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attachment_id = media_handle_upload('thumbnail', $post_id);
if (!is_wp_error($attachment_id)) {
set_post_thumbnail($post_id, $attachment_id);
header("Location: " . $_SERVER['HTTP_REFERER'] . "/?files=uploaded");
}
More info (see nonces for security too): https://codex.wordpress/Function_Reference/media_handle_upload