From: Laury GvR Date: Thu, 28 Apr 2016 19:12:59 +0000 (-0400) Subject: is_in_tree for front and template auto-inclusion for Members Only X-Git-Tag: v2.0.0^2~26^2~8 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=340125449d4f85ce1dae71f0255c9cf907c6b202;p=WP-Plugins%2Fglm-member-db.git is_in_tree for front and template auto-inclusion for Members Only --- diff --git a/setup/frontHooks.php b/setup/frontHooks.php index 30075767..52bcf00a 100644 --- a/setup/frontHooks.php +++ b/setup/frontHooks.php @@ -58,4 +58,46 @@ if (is_plugin_active($plugin_name)) { $trail->breadcrumbs = array(); } }); +} + + +/* + * This function returns true when the current page is the page given by ID + * or a descendent thereof. + */ +if (!function_exists('is_in_tree')) { + function is_in_tree( $pid ) { + global $post; + + if ( is_page($pid) ) + return true; + + $anc = get_post_ancestors( $post->ID ); + foreach ( $anc as $ancestor ) { + if( is_page() && $ancestor == $pid ) { + return true; + } + } + return false; + } +} +/* + * This function will make any page that is under the Members Only page + * (derived from an option) use the members only template when displaying, + * regardless of what template is selected in Admin side. + */ +add_filter( 'template_include', 'glm_members_only_template', 99 ); +function glm_members_only_template( $template ) { + $templateName = "glm-members-only-template.php"; + $membersOnlyPage = get_option('glm_members_database_members_only_id'); + $newTemplate = locate_template( $templateName ); + if ( is_in_tree($membersOnlyPage) && $newTemplate != '' ) { // if in same tree as the members page for this site + return $newTemplate; + /* Uncomment this if you want every child page of the members only page to be given the members only template + * (i.e. changed in the database) and not just using them on the front-end + */ + // global $post; + // update_post_meta($post->ID, '_wp_page_template', $templateName); + } + return $template; } \ No newline at end of file