NickOpris
NickOpris

Reputation: 559

Allow file access only if they belong to a published post/page in WordPress

I need to know if there is a plugin that limits access to uploaded files based on the status of the post/page they are related to.

In other words, when I upload an image into a post but the post is not published, I need WordPress to not allow access to that image until I'm switching the post to Published.

I can code this myself so please let me know if there is a plugin already so I can save time.

Upvotes: 0

Views: 160

Answers (1)

Obmerk Kronen
Obmerk Kronen

Reputation: 15969

well, I do not know of any specific plugin that will do that , but what you will need to do is actually change the .htaccess direct access file permissions in order to achieve URL restrictions.

There is a very nice article written by Stephan Harris that treats this issue in wordpress with several methods .

http://www.stephenharris.info/2012/restricting-direct-access-to-files-in-wordpress/

In his article, Stephan Harris mentioned those method to prevent direct access . I still find it difficult to imagine a case where this will be needed on a published / not published basis .

But still - you can easily check for post-status (with get_post_status() )and apply it in his script / methods to achieve what you want . look in particular at the method where he redirects ALL the (IMAGE) request to go through the index.php - there you can easily insert a permission check.

A second method that you can apply is to CHANGE the upload folder , thus making it more difficult for people to "guess" , as you said, the URL. That can be done on a post-by-post basis (every post different folder) or by a random string .

you can do that by a variety of ways, one of which is described here :

http://codex.wordpress.org/Function_Reference/wp_upload_dir

or simply use a plugins like THIS ONE that will give a folder name based on a post title . which is unknown to the non-authorized user until it is published.)

Upvotes: 1

Related Questions