Reworked how we're checking for our rewrite rules and flusing the cache.
authorChuck Scott <cscott@gaslightmedia.com>
Fri, 17 Jun 2016 19:29:44 +0000 (15:29 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 20 Jun 2016 13:26:25 +0000 (09:26 -0400)
setup/frontHooks.php

index 6a80ea8..2b2f979 100644 (file)
  *  Also note that parameters will be in the context of the main front controller constructor.
   */
 
-// Setup rewrite for member detail pages
-add_filter('rewrite_rules_array', function($rules) {
-    $newrules = array();
-    $newrules['('.$this->config['settings']['canonical_member_page'].')/([^/]*)$'] = 'index.php?pagename=$matches[1]&memberslug=$matches[2]';
-    return $newrules + $rules;
-});
+// Check if the canonical member page rewrite rule is not currently in place.
+$rules = get_option( 'rewrite_rules' );
+if ( ! isset( $rules['('.$this->config['settings']['canonical_member_page'].')/([^/]*)$'] ) ) {
+
+    // Setup rewrite for member detail pages
+    add_filter('rewrite_rules_array', function($rules) {
+        $newrules = array();
+        $newrules['('.$this->config['settings']['canonical_member_page'].')/([^/]*)$']='index.php?pagename=$matches[1]&memberslug=$matches[2]';
+        return $newrules + $rules;
+    });
+
+    // Rewrite is not in place, so setup init call to add it and flush the rewrite cache
+    add_filter('init', function() {
+        global $wp_rewrite;
+        $wp_rewrite->flush_rules();
+    });
+
+}
 
 // Add memberslug query var
 add_filter('query_vars', function($vars) {
@@ -39,15 +51,6 @@ add_filter('query_vars', function($vars) {
     return $vars;
 });
 
-// On init, check if we need to flush the rewrite rules to pickup the new ones
-add_filter('init', function() {
-    $rules = get_option( 'rewrite_rules' );
-    if ( ! isset( $rules['('.$this->config['settings']['canonical_member_page'].')/([^/]*)$'] ) ) {
-        global $wp_rewrite;
-        $wp_rewrite->flush_rules();
-    }
-});
-
 // Squash Bread Crumbs if using Breadcrumb NavXT plugin -- NEED TO CREATE A MANAGEMENT OPTION FOR THIS TO TURN IT ON/OFF
 $plugin_name = 'glm-member-db/glm-member-db.php';
 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );