NGG breaks WordPress’ default “template_redirect” actions
-
Hello NGG,
Among other problematic behaviour (such as how this plugin resolves the “document root”), this plugin (as of v3.36) disables WordPress’ default actions on the
template_redirect
hook:wp_old_slug_redirect
redirect_canonical
Replacing them with an early action to resolve the request URI (
$_SERVER['NGG_ORIG_REQUEST_URI']
). If this server variable is undefined, the method half-heartedly attempts to directly call the original action functions but only if BuddyPress and WPML are not installed.class M_WordPress_Routing extends C_Base_Module { # ... function _register_hooks() { add_action('template_redirect', array(&$this, 'restore_request_uri'), 1); // These two things cause conflicts in NGG. So we temporarily // disable them and then reactivate them, if they were used, // in the restore_request_uri() method if (has_action('template_redirect', 'wp_old_slug_redirect')) { remove_action( 'template_redirect', 'wp_old_slug_redirect'); } if (has_action('template_redirect', 'redirect_canonical')) { remove_action( 'template_redirect', 'redirect_canonical'); } } # ... function restore_request_uri() { if (isset($_SERVER['NGG_ORIG_REQUEST_URI'])) { $request_uri = $_SERVER['NGG_ORIG_REQUEST_URI']; $_SERVER['UNENCODED_URL'] = $_SERVER['HTTP_X_ORIGINAL_URL'] = $_SERVER['REQUEST_URI'] = $request_uri; if (isset($_SERVER['ORIG_PATH_INFO'])) { $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO']; } } // this is the proper behavior but it causes problems with WPML else { if (self::$_use_old_slugs) wp_old_slug_redirect(); if (self::$_use_canonical_redirect) redirect_canonical(); } }
class M_Third_Party_Compat extends C_Base_Module { # ... function _register_hooks() { # ... // Like WPML, BuddyPress is incompatible with our routing hacks. if (function_exists('buddypress')) { M_WordPress_Routing::$_use_canonical_redirect = FALSE; M_WordPress_Routing::$_use_old_slugs = FALSE; } // WPML fix if (class_exists('SitePress')) { M_WordPress_Routing::$_use_canonical_redirect = FALSE; M_WordPress_Routing::$_use_old_slugs = FALSE; add_action('template_redirect', array(&$this, 'fix_wpml_canonical_redirect'), 1); } # ... } # ... function fix_wpml_canonical_redirect() { M_WordPress_Routing::$_use_canonical_redirect = FALSE; M_WordPress_Routing::$_use_old_slugs = FALSE; }
There must be a better way to handle this.
Maybe by checking if the default
template_redirect
actions are present during your earlyrestore_request_uri
callback.For the project I’m working on, I implemented a complicated check to verify if NGG and WPML is disabling the default actions:
add_action( 'template_redirect', function () : void { global $sitepress; if ( isset( $_SERVER['NGG_ORIG_REQUEST_URI'] ) || ! class_exists( 'M_WordPress_Routing', false ) ) { return; } $has_wp_old_slug_redirect = has_action( 'template_redirect', 'wp_old_slug_redirect' ); $has_wp_redirect_canonical = has_action( 'template_redirect', 'redirect_canonical' ); if ( ! M_WordPress_Routing::$_use_old_slugs && ! $has_wp_old_slug_redirect ) { add_action( 'template_redirect', 'wp_old_slug_redirect' ); } if ( ! M_WordPress_Routing::$_use_canonical_redirect && ! $has_wp_redirect_canonical ) { $is_wpml_disabling_redirect_canonical = ( /** @see WPML_Root_Page_Actions::wpml_home_url_setup_root_page() */ ( class_exists( 'WPML_Root_Page', false ) && WPML_Root_Page::is_current_request_root() && ! WPML_Root_Page::uses_html_root() ) || /** @se: WPML_Redirect_By_Param::template_redirect_action() */ ( $sitepress && $sitepress->get_wp_api()->is_front_page() && $sitepress->get_wp_api()->get_query_var( 'page' ) && $sitepress->get_default_language() !== $sitepress->get_current_language() ) ); if ( ! $is_wpml_disabling_redirect_canonical ) { add_action( 'template_redirect', 'redirect_canonical' ); } } }, 2 );
One thing I noticed with WPML v4.6, the
wp_old_slug_redirect
action is not disabled as far as I can see.Regards,
- The topic ‘NGG breaks WordPress’ default “template_redirect” actions’ is closed to new replies.