最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Attaching a file to a page without adding to media library?

programmeradmin1浏览0评论

We are using WP for our new intranet, which will primarily be used for documentation and archiving. We have some pages where we want to attach PDFs, documents, images, etc. However, we do not want these media items to be added to the media library when we attach them to a page; i.e., if the page is deleted, the media items are also deleted. Is there a way to do this?

Thanks!

We are using WP for our new intranet, which will primarily be used for documentation and archiving. We have some pages where we want to attach PDFs, documents, images, etc. However, we do not want these media items to be added to the media library when we attach them to a page; i.e., if the page is deleted, the media items are also deleted. Is there a way to do this?

Thanks!

Share Improve this question asked Jan 7, 2021 at 19:39 RayRay 1 1
  • Media items shouldn't be deleted when a post or a page is deleted. If you're hosting them somewhere else you can just paste the URL/path the file. – Tony Djukic Commented Jan 7, 2021 at 21:29
Add a comment  | 

1 Answer 1

Reset to default 0

There is no such a solution out of the box, and any files uploaded to WP are publically available to anyone who can access the domain, so you might want to keep that in mind.

You'll need to code a solution, possibly as follows:

  • on post creation, use wp_insert_post to create the require folder.
  • Use something like glob($root_folder.$post_slug . "/*.{jpg,jpeg,png,pdf}",GLOB_BRACE) to list all files in the post-specific folder . (Note, if the slug is ever changed, you'll need to manually rename the folder or automate it somehow.
  • Use before_delete_post to delete the folder & all files.
发布评论

评论列表(0)

  1. 暂无评论