如何在wp_ins_post的同时上传帖子缩略图?

时间:2018-02-16 作者:wpdev

我正在尝试在前端提交帖子。无需上传和设置缩略图即可正常工作。这是我的html表单。

<form method="post" action="">
    <input type="text" name="title">
    <textarea name="content"></textarea>
    <input type="file" name="thumbnail">
    <input type="submit" name="submit">
</form>
这是我的php代码。

<?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");
    }
}
?>
它创建post ok。但它不会设置任何缩略图。我不知道我错在哪里。

3 个回复
最合适的回答,由SO网友:F. Gálvez 整理而成

在表单中,需要代码enctype=“多部分/表单数据”

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

SO网友:Syborg

您可以使用wp\\u insert\\u attachment()函数提供的示例codex page 和set\\u post\\u缩略图()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 );
    }
}

SO网友:Stefano Tombolini

似乎有一些不必要的代码。请尝试以下方法:

$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");
    }
更多信息(有关安全性,请参阅nonces):https://codex.wordpress.org/Function_Reference/media_handle_upload

结束

相关推荐

为什么PRE_GET_POSTS在POST类型存档中运行良好,但在搜索结果列表中不起作用?

我已经用pre_get_posts 过滤器(如下所示),通过自定义字段“startdate2”按日期排序。这适用于WooCommerce产品列表,但不影响通过相同模板显示的搜索结果列表的排序(archive-product.php).搜索结果中的产品帖子总是顺序错误。我试了很多次,但都没用,我也不知道为什么。是不是pre_get_post 筛选器不适用于搜索结果?还是有必要再写一次pre_get_post 是否仅筛选搜索结果列表?我还在模板中尝试了一个额外的查询,只针对“is\\u search()”-但