Make images private in Wordpress

admin

Administrator
Staff member
I'm making a site, that I would like to make private. The most important part, is that the images on the domain can't be seen, without the user logging in first.
So I would like all traffic to be redirected to www.DOMAINNAME.com/wp-admin (also for images), if the user isn't logged in.

Here's what I've tried:

1) Plugins. I've tried both <a href="https://wordpress.org/plugins/wp-force-login/" rel="nofollow noreferrer">Wordpress Force Login</a> , the plugin <a href="https://wordpress.org/plugins/wp-require-login/" rel="nofollow noreferrer">wp-require-login</a> and a <a href="https://wordpress.org/plugins/coming-soon/" rel="nofollow noreferrer">Coming soon page and Maintenance mode</a>.

2) Adding a function from <a href="https://stackoverflow.com/questions...og-completely-invisible-to-public-not-private">this answer</a>. Which is this:

Code:
function is_login_page() {
    return in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) );
}

function wpse_make_blog_private() {
    if ( ! is_user_logged_in() &amp;&amp; ! is_admin() &amp;&amp; ! is_login_page() ) { 
    global $wp_query;
    $wp_query-&gt;set_404();
    }
}
add_action( 'wp', 'wpse_make_blog_private' );

Non of these things redirects the traffic, if I go to the direct URL for the image (such as <a href="http://www.DOMAINNAME.com/uploads/2015/10/foobar.jpg" rel="nofollow noreferrer">http://www.DOMAINNAME.com/uploads/2015/10/foobar.jpg</a> ).

Can that be done?

----------------- EDIT 1 --------------

Mevius pointed out, that Wordpress might not be loaded, if you type in the direct URL to an image, so he suggests, that it should be done on apache-level.

------------- END OF EDIT 1 -----------