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
1 Answer
Reset to default 0There 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.