$post) $items[$key]->url = the_download_link($post->url, $post->object_id); return $items; } /** * Check if post if members only content and redirects if necessary. * * @return bool */ public function is_members_only() { global $post; $post_check = $this->allowed_post_types; // desired post types to restrict if ( !in_array(get_post_type(), $post_check) ) return; if ( !is_member() && !headers_sent() && (is_single() || is_page())) { // not an approved or logged in user if ( (boolean)$post->_members_only ) { wp_redirect( home_url() ); exit; } } else { return (boolean)$post->_members_only; } } /** * Filter the permalink based on the post type. * * @return bool */ function the_download_link($url, $post = null) { if ( is_null($post)) { global $post; } $members_only = is_object($post) ? $post->_members_only : get_post_meta($post, "_members_only", true); if ($members_only && !is_member()) return "#login"; $post_type = get_post_type( $post ); switch( $post_type ) { case "attachment": $has_txt_link = ($post->post_mime_type == "text/plain"); $attachment_link = wp_get_attachment_url($post->ID); return $has_txt_link ? get_data($attachment_link) : $attachment_link; break; default: return $url; } } } new WP_Members_Only();