-
-
Save cfxd/37d7864c2ac24e628bd1f5caa341c8a7 to your computer and use it in GitHub Desktop.
Class to make things members-only in WordPress.--This class depends on a custom capability called 'is_approved_member' for user member access and the custom post meta key '_members_only' as a post-level custom field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Wrapper for class method that will check if user is registered member | |
| * | |
| * @return boolean | |
| */ | |
| function is_member(){ | |
| return WP_Members_Only::is_member(); | |
| } | |
| class WP_Members_Only { | |
| var $allowed_post_types = array( 'post', 'page', 'attachment' ); | |
| function __construct($args = null) { | |
| // do any processing of data here and setup all filters and actions | |
| if ( is_admin() ) { | |
| add_filter('wp_get_nav_menu_items', array(&$this, 'nav_item_filter')); | |
| add_action('get_header', array(&$this, 'is_members_only')); | |
| add_filter('post_type_link', array(&$this, 'the_download_link'), 10, 2); | |
| add_filter('the_permalink', array(&$this, 'the_download_link')); | |
| } | |
| } | |
| /** | |
| * Check if user is registered member | |
| * | |
| * @return boolean | |
| */ | |
| public static function is_member(){ | |
| return current_user_can('is_approved_member'); | |
| } | |
| /** | |
| * Filter nav menu items that are members_only. | |
| * | |
| * @return array | |
| */ | |
| public function nav_item_filter($items){ | |
| foreach($items as $key => $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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment