4

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.

  • 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 Feb 17 '18 at 16:02
  • No. Media not uploading. Do you have any suggestion? – bilimokur Feb 17 '18 at 16:24
  • Check the answer I proposed. – Stefano Tombolini Feb 17 '18 at 16:47
0

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.org/Function_Reference/media_handle_upload

  • Thank you. But it not uploads and sets thumbnail. – bilimokur Feb 17 '18 at 18:32
0

In form, you need the code enctype="multipart/form-data"

<form method="post" action="" enctype="multipart/form-data">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.