在wordpress媒体上传图片

时间:2014-03-07 16:14:43

标签: wordpress

上传图片如何在wordpress中使用前端表单中的<input type="file">

此刻我有这段代码:

<form method="post">

<input type="text" />
<input type="text" />
<input type="file" />
<input type="submit" />

</form>

2 个答案:

答案 0 :(得分:0)

在functions.php中

<?php
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;
}
?>

在您的主题文件中:

<?php
if ($_FILES) {

foreach ($_FILES as $file => $array) {
    $newupload = insert_attachment($file,$pid);
}
};
?>

答案 1 :(得分:0)

实际上,您有一个更新的代码,关于如何在wordpress网站上使用media_handle_upload()函数:https://codex.wordpress.org/Function_Reference/media_handle_upload

它在其上传表单中使用<input type="file">