is_in_tree for front and template auto-inclusion for Members Only
authorLaury GvR <laury@gaslightmedia.com>
Thu, 28 Apr 2016 19:12:59 +0000 (15:12 -0400)
committerLaury GvR <laury@gaslightmedia.com>
Thu, 28 Apr 2016 19:12:59 +0000 (15:12 -0400)
setup/frontHooks.php

index 3007576..52bcf00 100644 (file)
@@ -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