如何使用 WordPress 自定义字段/元框附加文件?

发布于 2024-08-27 09:37:59 字数 861 浏览 16 评论 0原文

我正在使用 WordPress 的 add_meta_box() 函数将自定义元字段添加到“添加新帖子”页面, 像这样。我希望其中一个字段允许用户上传文件,以便单个图像、pdf、音频文件或视频可以与帖子关联。

我见过的最接近的例子是这个(链接已删除*)。不幸的是它不适合我的需要,因为我希望我的文件由 Wordpress 的媒体上传器处理 - 所以它应该随后出现在媒体库中,并且应该根据媒体设置生成缩略图。

我认为理想情况下会有一种方法可以利用 WordPress 现有的“添加媒体”对话框,并将上传文件的 URL 简单地输出到文本框中,但我不知道如何做到这一点。 这个问题类似,但答案有点笨拙 - 我想让我的最终用户保持这个超级简单。

我怎样才能做到这一点?请不要推荐 Flutter 或 Magic Fields 等插件 - 我已经尝试过这些插件,但它们不适合我的目的(我希望图像由 Wordpress 的媒体上传器处理)。我正在使用 WordPress 3.0-alpha。

(*=由于报告恶意软件而删除了链接;它位于存档中,但风险自负)

I am using Wordpress's add_meta_box() function to add customized meta fields to the Add New Post page, like this. I want one of these fields to allow the user to upload a file, so that a single image, pdf, audio file, or video can be associated with the post.

The closest example I've seen is this one (link removed*). Unfortunately it does not suit my needs, as I want my file to be processed by Wordpress's Media Uploader - so it should appear in the Media Library afterwards, and thumbnails should be generated according to the Media settings.

I think ideally there would be a way to tap into Wordpress's existing Add Media dialog, and simply output the URL of the uploaded file into a text box, but I don't see how to do that. This question is similar, but the answers are a little clunky - I would like to keep this super simple for my end users.

How can I accomplish this? Please do not recommend plugins such as Flutter or Magic Fields - I have tried these and they do not suit my purposes (I want the images to be processed by Wordpress's Media Uploader). I am using Wordpress 3.0-alpha.

(*=link removed due to report of malware; it is in the archive, but at own risk)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾`听者〃 2024-09-03 09:37:59

我最终自己解决了这个问题,并让它按照我想要的方式工作,并且我想我会为任何发现这个问题并且好奇的人发布更新。

我没有使用任何插件,而是编写了一个自定义函数来处理我的主题的文件上传。我使用了内置的 WordPress 函数,例如 save_custom_postdata()、wp_handle_upload()、wp_insert_attachment()、wp_generate_attachment_metadata() 和 wp_update_attachment_metadata()。

我还必须使用 add_action('admin_head'...) 添加一些 JavaScript,将“multipart/form-data”添加到“编辑帖子”页面上的表单标记,以允许文件上传。

我会发布我的代码,但它很长并且特定于我的项目。

I eventually figured this out for myself and got it working just as I wanted, and thought I would post an update for anyone who finds this question and is curious.

I did not use any plugins, but wrote a custom function to handle file uploads for my theme. I used built-in Wordpress functions such as save_custom_postdata(), wp_handle_upload(), wp_insert_attachment(), wp_generate_attachment_metadata() and wp_update_attachment_metadata().

I also had to use add_action('admin_head'...) to add some JavaScript which adds "multipart/form-data" to the form tag on the Edit Post page, to allow file uploads.

I would post my code, but it is very long and specific to my project.

耳根太软 2024-09-03 09:37:59

您无需使用 JavaScript 来编辑表单。这可以使用 post_edit_form_tag 函数来完成,如下所示:

add_action( 'post_edit_form_tag' , 'post_edit_form_tag' );

function post_edit_form_tag( ) {
    echo ' enctype="multipart/form-data"';
}

更多详细信息(包括示例)可在 http 上找到://codex.wordpress.org/Plugin_API/Filter_Reference/post_edit_form_tag

You needn't use JavaScript to edit the form. This is possible to do using the post_edit_form_tag function like so:

add_action( 'post_edit_form_tag' , 'post_edit_form_tag' );

function post_edit_form_tag( ) {
    echo ' enctype="multipart/form-data"';
}

More details (including an example) are available at http://codex.wordpress.org/Plugin_API/Filter_Reference/post_edit_form_tag.

寄居人 2024-09-03 09:37:59

看看 图像小部件 - 一个制作精良的小部件,可以满足您的需求,但作为一个小部件。也许您可以将其转换为在“添加新帖子”页面上工作。

您可以尝试的另一个插件是 Pods CMS。可能有点矫枉过正,但是当您用它上传文件时,它们会自动添加到媒体库中。

Take a look at Image Widget - a well made widget that does what you want, but as a widget. Maybe you can convert it to work on your Add new post page.

Another plugin you can try is Pods CMS. Might be overkill but when you do fileuploads with this, they are automaticly added to the media library.

北陌 2024-09-03 09:37:59

这个 WordPress Meta Box PHP Helper 类可能会在您使用 WordPress Meta Box 时有所帮助。

This WordPress Meta Box PHP Helper class might help you when working with WordPress Meta Boxes.

骷髅 2024-09-03 09:37:59

我认为这就是您正在寻找的: http://wordpress.org/extend/plugins/ nkmimagefield/
显然它不支持自定义帖子类型,但应该进行一些修改。它已经完成了与媒体编辑器交互的艰苦工作。

I think this is what you're looking for: http://wordpress.org/extend/plugins/nkmimagefield/
Apparently it doesn't support custom post types but a little hacking should do. It already does the hard work of interfacing with the media editor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文