从前端表单设置特色图像

时间:2016-03-13 09:22:42

标签: php wordpress validation file-upload

所以我一直在网上搜索几天寻找解决这个问题的方法。我正在尝试从此表单的上传输入设置帖子的精选图像:

<form method="post" action="" id="quick-post-form" enctype="multipart/form-data">
    <textarea id="input-box" placeholder="What's on your mind?" maxlength="10000" name="quick-post-area" class="col-xs-12"></textarea>
    <!-- images -->
    <fieldset class="images">
    <label for="images">Upload an image</label>
    <input type="file" name="image-uploader" id="img-upload" size="50">
    </fieldset>
    <!-- images end -->
    <input type="submit" value="Post" name="submit" class="col-md-4 col-xs-12" id="quick-post-submit">
</form>

我正在使用wp插入帖子从此表单创建帖子,但图片未按原样上传。这是我正在使用的PHP:

<?php 
function quick_post() {

    // Initialize the page ID to -1. This indicates no action has been taken.
    $post_id = -1;

    $author_id = 1;
    $slug = 'post';
    global $current_user;
    $user_id = get_current_user_id();

    // If the page doesn't already exist, then create it
    if( null == get_page_by_title( $title ) ) {

        $post_id = wp_insert_post(
            array(
                'comment_status'    =>  'open',
                'ping_status'       =>  'closed',
                'post_author'       =>  $user_id,
                'post_name'         =>  $slug,
                'post_title'        =>  'Post By: ' . $current_user->user_login,
                'post_status'       =>  'publish',
                'post_type'         =>  'post',
                'post_content'      =>  $_POST['quick-post-area'],
                'post_category' => array( 3 ),
            )
        );

require_once (ABSPATH.'/wp-admin/includes/media.php');
require_once (ABSPATH.'/wp-admin/includes/file.php');
require_once (ABSPATH.'/wp-admin/includes/image.php');
$attachmentId = media_handle_upload('image-uploader', $post_ID);
set_post_thumbnail($post_ID, $attachmentId);


    // Otherwise, we'll stop
    } else {

            // Arbitrarily use -2 to indicate that the page with the title already exists
            $post_id = -2;

    } // end if

} // end programmatically_create_post

if(isset($_POST['submit']))
{
   quick_post();
   header('Location: https://outrankd.com');
}
?>

我还应该提一下,一旦这个工作,我打算使用Wordpress检查文件类型函数来验证上传,但认为这应该适用于这样的事情:

if ($attachmentId = wp_check_filetype('image')) {
set_post_thumbnail($post_ID, $attachmentId);
}

如果有人能够解释我出错的地方,我们将非常感激。

由于

0 个答案:

没有答案