Cleaning up some issues with rewrites.
authorChuck Scott <cscott@gaslightmedia.com>
Fri, 14 Apr 2017 20:06:18 +0000 (16:06 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Fri, 14 Apr 2017 20:06:18 +0000 (16:06 -0400)
setup/frontHooks.php

index bcc1f19..d0791ac 100644 (file)
  *  *** Also note that parameters will be in the context of the main front controller constructor. ***
  */
 
-
 // Setup rewrite for package detail page
+define( 'GLM_PACKAGING_REWRITE_RULE', '('.$this->config['settings']['canonical_package_page'].')/([^/]*)$');
 add_filter( 'rewrite_rules_array', function($rules) {
     $newrules = array();
-    $newrules['('.$this->config['settings']['canonical_package_page'].')/([^/]*)$']='index.php?pagename=$matches[1]&packageslug=$matches[2]';
+    $newrules[GLM_PACKAGING_REWRITE_RULE]='index.php?pagename=$matches[1]&packageslug=$matches[2]';
     return $newrules + $rules;
 });
 add_filter( 'query_vars', function($vars) {
@@ -38,11 +38,14 @@ add_filter( 'query_vars', function($vars) {
     return $vars;
 });
 add_action('wp_loaded', function() {
-    $rules = get_option('rewrite_rules');
-    if ( ! isset( $rules['('.$this->config['settings']['canonical_package_page'].')/([^/]*)$'] ) ) {
-        trigger_error('Doing rewrite flush - Package detail page', E_USER_NOTICE); // Logging that this is happening so we can track when that happens.
-        global $wp_rewrite;
-        $wp_rewrite->flush_rules();
+    // Only do this if this is a front-end request
+    if (!is_admin()) {
+        $rules = get_option('rewrite_rules');
+        if ( ! isset( $rules[GLM_PACKAGING_REWRITE_RULE] ) ) {
+            trigger_error('Doing rewrite flush - Package detail page', E_USER_NOTICE); // Logging that this is happening so we can track when that happens.
+            global $wp_rewrite;
+            $wp_rewrite->flush_rules();
+        }
     }
 });