从带有图像的表单创建帖子

时间:2011-07-12 作者:DennisT

我在Wordpress Answers网站上发现了这个由@Bainternet创建的奇妙脚本,它可以将图像从html表单附加到帖子中。。

我试图将in合并到我的“要发布的html表单”脚本中。。我确实在后端创建了一个post as draft。。但图像不会显示,也不会显示在“媒体”中。。

这是@Bainternet脚本/答案,Submit post and upload image from front-end

这是我的,

<?php 


if(isset($_POST[\'new_post\']) == \'1\') {
$post_title = $_POST[\'post_title\'];
$post_category = \'fashion\';
$post_content = $_POST[\'post_content\'];

$new_post = array(
      \'ID\' => \'\',
      \'post_author\' => $user->ID, 
      \'post_category\' => array($post_category),
      \'post_content\' => $post_content,
      \'post_title\' => $post_title,
      \'post_status\' => \'draft\'
    );

$post_id = wp_insert_post($new_post);


        if (!function_exists(\'wp_generate_attachment_metadata\')){
            require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
        }
         if ($_FILES) {
            foreach ($_FILES as $file => $array) {
                if ($_FILES[$file][\'error\'] !== UPLOAD_ERR_OK) {
                    return "upload error : " . $_FILES[$file][\'error\'];
                }
                $attach_id = media_handle_upload( $file, $post_id );
            }   
        }
        if ($attach_id > 0){
            //and if you want to set that image as Post  then use:
            update_post_meta($post_id,\'_thumbnail_id\',$attach_id);
        }


// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect( \'http://domain.com\' );
}      
?>      

<form method="post" action=""> 
<input type="text" name="post_title" size="45" id="input-title" value="heading"/><br>
<input type="text" name="post_url" size="45" id="input-url" value="Url"/><br>
<input type="file" name="thumbnail" id="thumbnail"><br>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea> <br>
<input type="hidden" name="new_post" value="1"/> <br>
<input class="subput round" type="submit" name="submit" value="Post"/><br>
</form>

3 个回复
最合适的回答,由SO网友:Bainternet 整理而成

我想问题是你的表单标签丢失了enctype="multipart/form-data" 因此,请尝试更改:

<form method="post" action=""> 
收件人:

<form method="post" action="" enctype="multipart/form-data"> 
另外,如果要重定向到新创建的post更改,请执行以下操作:

wp_redirect( \'http://domain.com\' );
收件人:

wp_redirect( get_permalink($post_id));
exit();
更新:从评论中我了解到,我希望图像显示在内容中,这是另一回事,但不是那么难,

更改:

if ($attach_id > 0){
            //and if you want to set that image as Post  then use:
            update_post_meta($post_id,\'_thumbnail_id\',$attach_id);
        }
收件人:

if ($attach_id > 0){
    $post = get_post($post_id,\'ARRAY_A\');
    $image = wp_get_attachment_image_src( $attach_id );
    $image_tag = \'<img src="\'.$image[0].\'" width="\'.$image[1].\'" height="\'.$image[2].\'" />\';

    //add image above the content
    $post[\'post_content\'] = $image_tag . $post[\'post_content\'];

    //add image under the content
    //$post[\'post_content\'] = $post[\'post_content\'] . $image_tag;

    $post_id =  wp_update_post( $post );
}

SO网友:DennisT

对于每个需要相同脚本的人。

它的作用是,一个简单的html表单将图像和一些文本上传到wordpress和media gallery,并根据发布的内容创建一篇文章作为草稿。并将您重定向到创建的帖子进行预览。

非常感谢@Bainternet

<?php 


if(isset($_POST[\'new_post\']) == \'1\') {
$post_title = $_POST[\'post_title\'];
$post_category = \'todays_post\';
$post_content = $_POST[\'post_content\'];

$new_post = array(
      \'ID\' => \'\',
      \'post_author\' => $user->ID, 
      \'post_category\' => array($post_category),
      \'post_content\' => $post_content,
      \'post_title\' => $post_title,
      \'post_status\' => \'draft\'
    );

$post_id = wp_insert_post($new_post);


        if (!function_exists(\'wp_generate_attachment_metadata\')){
            require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
        }
         if ($_FILES) {
            foreach ($_FILES as $file => $array) {
                if ($_FILES[$file][\'error\'] !== UPLOAD_ERR_OK) {
                    return "upload error : " . $_FILES[$file][\'error\'];
                }
                $attach_id = media_handle_upload( $file, $post_id );
            }   
        }
if ($attach_id > 0){
    $post = get_post($post_id,\'ARRAY_A\');
    $image = wp_get_attachment_image_src( $attach_id, \'large\' );
    $image_tag = \'<p><img src="\'.$image[0].\'" width="\'.$image[1].\'" height="\'.$image[2].\'" /></p>\';

    //add image under the content
    $post[\'post_content\'] = $image_tag . $post[\'post_content\'];

    //add image above the content
    //$post[\'post_content\'] = $post[\'post_content\'] . $image_tag;

$post_id =  wp_update_post( $post );
}


// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect( get_permalink($post_id));
exit();
}      
?>
<html>
<head>
<title>Form</title>

</head>
<body>

<form method="post" action="" enctype="multipart/form-data">
<input type="text" name="post_title" size="45" id="input-title" value="heading"/><br>
<input type="text" name="post_url" size="45" id="input-url" value="Url"/><br>
<input type="file" name="thumbnail" id="thumbnail"><br>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea> <br>
<input type="hidden" name="new_post" value="1"/> <br>
<input class="subput round" type="submit" name="submit" value="Post"/><br>
</form>

</body>
</html>

SO网友:Jamie

我已经尝试过这个代码,当我尝试时,什么都没有发生。我还没有使用这个图片,我只是想用这个表格来写一篇新文章。但表单变量似乎是空的。

我使用var\\u转储进行检查,但我的表单中没有任何内容。在最新版本的wordpress中,上述代码对我不起作用。我不得不把它改成这个

$my_post = array();
    $my_post[\'post_title\'] = "My Post"
    $my_post[\'post_content\']  = \'This is my post.\';
    $my_post[\'post_status\']   = \'publish\';
    $my_post[\'post_author\']   = 1;
    $my_post[\'post_category\'] = array(9);
这就是我做测试的方式,它确实是这样工作的,只有帖子类别不能与slug一起工作,这可能是一个问题。每当我尝试用$\\u post[\'name\']替换post\\u title时,代码都会失败。这是我的表格

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

    <table width="474" border="0" cellspacing="0" cellpadding="0" id="reserve">
        <tr>
                            <td class="formInput"><input type="text" id="name" name="name" /></td>

结束