如何将媒体从前端添加到现有帖子?

时间:2011-05-18 作者:m-torin

Golden Apples Design 创建了(据我所知)媒体上传功能,许多人在WP上。A和其他地方似乎建议。但所有的问答;正如我在StackExchange上看到的那样,我同意创建一个全新的帖子。

从前端上传媒体、将其附加到当前帖子、生成适当的缩略图,然后刷新[库]中显示新图像的页面的最佳方式是什么?

下面是Than的代码:在函数文件中。。。

function insert_attachment($file_handler,$post_id,$setthumb=\'false\') {

  // check to make sure its a successful upload
  if ($_FILES[$file_handler][\'error\'] !== UPLOAD_ERR_OK) __return_false();

  require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
  require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
  require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');

  $attach_id = media_handle_upload( $file_handler, $post_id );

  if ($setthumb) update_post_meta($post_id,\'_thumbnail_id\',$attach_id);
  return $attach_id;
在页面模板标题中。。。

// set $post_id to the id of the post you want to attach
// these uploads to (or \'null\' to just handle the uploads
// without attaching to a post)

if ($_FILES) {
  foreach ($_FILES as $file => $array) {
    $newupload = insert_attachment($file,$post_id);
    // $newupload returns the attachment id of the file that
    // was just uploaded. Do whatever you want with that now.
  }
}
上载使用的表单。。。

<form method="post" action="#" enctype="multipart/form-data" >
  <input type="file" name="an_uploaded_attachment">
  <input type="file" name="another_uploaded_attachment">
  <input type="file" name="yet_another_uploaded_attachment">
  <input type="submit">
<form>

2 个回复
最合适的回答,由SO网友:m-torin 整理而成

@实际上是AboSamianswered this question 在我的搜索调查中没有出现的一个旧帖子中。虽然他实际上在寻找其他东西,但他的示例代码工作得很好。

下面是脚本:

<?php $post_id = $post->ID;
if ( isset( $_POST[\'html-upload\'] ) && !empty( $_FILES ) ) {
    require_once(ABSPATH . \'wp-admin/includes/admin.php\');
    $id = media_handle_upload(\'async-upload\', $post_id); //post id of Client Files page
    unset($_FILES);
    if ( is_wp_error($id) ) {
        $errors[\'upload_error\'] = $id;
        $id = false;
    }

    if ($errors) {
        echo "<p>There was an error uploading your file.</p>";
    } else {
        echo "<p>Your file has been uploaded.</p>";
    }
}

?>
    <form id="file-form" enctype="multipart/form-data" action="<?php echo $_SERVER[\'REQUEST_URI\']; ?>" method="POST">

    <p id="async-upload-wrap"><label for="async-upload">upload</label>
    <input type="file" id="async-upload" name="async-upload"> <input type="submit" value="Upload" name="html-upload"></p>

    <p><input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id ?>" />
    <?php wp_nonce_field(\'client-file-upload\'); ?>
    <input type="hidden" name="redirect_to" value="<?php echo $_SERVER[\'REQUEST_URI\']; ?>" /></p>

    <p><input type="submit" value="Save all changes" name="save" style="display: none;"></p>
    </form>

SO网友:Hameedullah Khan

在标题更改中$post_id$post->ID.

// set $post_id to the id of the post you want to attach
// these uploads to (or \'null\' to just handle the uploads
// without attaching to a post)

if ($_FILES) {
  foreach ($_FILES as $file => $array) {
    $newupload = insert_attachment($file,$post->ID);
    // $newupload returns the attachment id of the file that
    // was just uploaded. Do whatever you want with that now.
  }
}

结束

相关推荐

404从wp-Content/Uploads/获取图像时

我在获取图像时获得404状态,http仍然包含该图像。图像显示在浏览器中,但404代码中断了一些应用程序。对wp内容/上载/的调用被重定向到。htaccess:<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\\.php$ - [L] RewriteRule (.*) /index.php?getfile=$1 [L] </IfModule>&