WordPress Media as a Custom Post Type

寵の児 提交于 2019-12-10 20:03:59

问题


I am building a WordPress website that will allow members to upload media such as videos, images, etc. They will do this on a public form at for example: domain.com/upload

The form will ask for a Title, a brief summary, show some categories to choose from, and finally a file uploader. The user will then be able to submit the form and it will appear on the site as a post but instead of being just text it is a piece of media.

I have created a custom post type and all seems to be fine, the problem I have is how to deal with the media upload, as the default WP method is to upload an item and then manually insert it into the post (and you can have as many inserted as you like). Where as I want users to be only able to upload one file per post and not have to insert the file into a post because the file itself is the post. They will also be able to upload a featured image.

Any ideas on how I can develop this? Thanks.


回答1:


You could use WP upload methods to upload your file to the server. Then (depending on your upload's media type) you would insert HTML representation of the file into the post (e.g. parse a text file, create tag for image or embed a video).

If you want to see some working example, please let me know. I just outline the flow:

  • wp-handle_upload() to physically upload a file
  • wp_update_post() or wp_insert_post() depending on whether you're creating a new user post or editing existing one. Both methods require one argument - Post object. That's where you put your content (as a post_content field of course) and post data


来源:https://stackoverflow.com/questions/4380037/wordpress-media-as-a-custom-post-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!