How to protect post attachments related to a custom post type, from non-logged in users, on 1 subsite of a multisite installation?

Andrew,

In the case where your custom post type exists on only one sub-site of your Multi Site installation, you can in your template file in which controls the display and output of your custom post type work with simple conditional functions such as;

  • is_singular(‘post_type_name’);
  • is_user_logged_in();

…and in-fact it might well be as simple as;

<?php if(is_user_logged_in()) : ?>

<?php echo wp_get_attachment_url( 12 );?>

<?php else : ?>

//Do what you want here, such as nothing or redirect to login page or alert user that some content is hidden etc.

<?php endif;?>

Hopefully this is along the lines of what you are looking for. If its not as simple as that for solution let me know and we’ll dig deeper.

PS. Replace wp_get_attachment_url() with whatever suits your purpose;

  • wp_get_attachment_metadata()
  • wp_read_image_metadata()
  • wp_get_attachment_image()
  • wp_get_attachment_image_src()
  • wp_get_attachment_thumb_file()
  • wp_get_attachment_thumb_url()
  • wp_get_attachment_link()
  • wp_get_attachment_url()

@Codex… HERE

NOTE

Or… in the case of using WPAlchemy you would replace the above wp_get_attachment… etc with your custom metabox code instead.

<?php custom_metabox->the_field('name'); ?>