如何使用自定义上传表单将文件上传到 wordpress?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8931870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I upload a file to wordpress using a custom upload form?
提问by jasonaburton
I don't want to use Wordpress' built in media uploader.
我不想使用 Wordpress 内置的媒体上传器。
I have a form (on the frontend) of my site, and I need to allow anyone to upload an image to my uploads folder in wp-content. I've found many tutorials, but they all tell me how to do it with the wordpress uploader. I need to be able to validate what users are uploading.
我的网站有一个表单(在前端),我需要允许任何人将图像上传到 wp-content 中的我的上传文件夹。我找到了很多教程,但它们都告诉我如何使用 wordpress 上传器进行操作。我需要能够验证用户上传的内容。
Any help is greatly appreciated!!!
任何帮助是极大的赞赏!!!
Thanks.
谢谢。
采纳答案by jasonaburton
Solved. This is the code I used to do it:
解决了。这是我用来做的代码:
In my code:
在我的代码中:
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('file-upload', $post->ID);
In my form:
以我的形式:
<input type="file" name="file-upload" id="file-upload" />
回答by mnelson7982
Does this help?
这有帮助吗?
WPTuts: Allow users to submit images to your site
PHP:
PHP:
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('file-upload', $post->ID);
HTML:
HTML:
<input type="file" name="file-upload" id="file-upload" />