Fix page routing conflicts preventing pages from loading

- Removed overly broad rewrite rules that were catching all trainer/* URLs
- Now only adds specific rewrite rules for non-page routes (like event/manage)
- Allows WordPress to handle actual page URLs naturally
- Created mu-plugin to fix routing for existing installations

This fixes the issue where pages were showing blog layout instead of content.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
bengizmo 2025-07-29 10:14:47 -03:00
parent b55b169750
commit 3f7820c487
3 changed files with 177 additions and 9 deletions

View file

@ -136,16 +136,10 @@ class HVAC_Route_Manager {
}
// Add rewrite rules for hierarchical URLs
// IMPORTANT: Only add rules for pages that don't exist as actual WordPress pages
$hierarchical_rules = array(
// Trainer pages
'trainer/?$' => 'index.php?hvac_route=trainer',
'trainer/([^/]+)/?$' => 'index.php?hvac_route=trainer&hvac_page=$matches[1]',
'trainer/([^/]+)/([^/]+)/?$' => 'index.php?hvac_route=trainer&hvac_page=$matches[1]&hvac_subpage=$matches[2]',
// Master trainer pages
'master-trainer/?$' => 'index.php?hvac_route=master-trainer',
'master-trainer/([^/]+)/?$' => 'index.php?hvac_route=master-trainer&hvac_page=$matches[1]',
'master-trainer/([^/]+)/([^/]+)/?$' => 'index.php?hvac_route=master-trainer&hvac_page=$matches[1]&hvac_subpage=$matches[2]',
// Only add rules for event manage subpages
'trainer/event/([^/]+)/?$' => 'index.php?hvac_route=trainer&hvac_page=event&hvac_subpage=$matches[1]',
);
foreach ($hierarchical_rules as $regex => $redirect) {

View file

@ -0,0 +1,97 @@
#!/bin/bash
echo "=== Disabling Conflicting Rewrite Rules ==="
# SSH into staging and comment out the problematic rules
ssh roodev@146.190.76.204 << 'EOF'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html
# Create a temporary fix by removing the conflicting rewrite rules
wp eval '
// Remove the trainer route rules that conflict with actual pages
remove_action("init", array(HVAC_Route_Manager::instance(), "register_rewrite_rules"), 5);
// Flush rewrite rules to clear them
flush_rewrite_rules();
'
# Now let's create a more targeted approach
echo "Creating targeted rewrite fix..."
# Create a mu-plugin to handle this properly
mkdir -p wp-content/mu-plugins
cat > wp-content/mu-plugins/hvac-fix-page-routing.php << 'PHP'
<?php
/**
* Fix HVAC page routing conflicts
*
* This ensures actual WordPress pages load instead of being caught by rewrite rules
*/
// Remove conflicting rewrite rules on init
add_action('init', function() {
global $wp_rewrite;
// Get current rules
$rules = $wp_rewrite->rules;
// Remove rules that conflict with actual pages
$conflicting_patterns = array(
'trainer/([^/]+)/?$',
'master-trainer/([^/]+)/?$',
);
foreach ($conflicting_patterns as $pattern) {
if (isset($rules[$pattern])) {
unset($rules[$pattern]);
}
}
$wp_rewrite->rules = $rules;
}, 999);
// Ensure pages load with their templates
add_filter('template_include', function($template) {
global $wp_query;
if (is_404()) {
// Check if this should be a page
$path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
$page = get_page_by_path($path);
if ($page && $page->post_status === 'publish') {
// Force WordPress to recognize this as a page
$wp_query->is_404 = false;
$wp_query->is_page = true;
$wp_query->is_singular = true;
$wp_query->queried_object = $page;
$wp_query->queried_object_id = $page->ID;
// Set up post data
setup_postdata($page);
// Get the correct template
$page_template = get_page_template_slug($page->ID);
if ($page_template) {
$located = locate_template(array($page_template));
if ($located) {
return $located;
}
}
// Fall back to page.php
return get_page_template();
}
}
return $template;
}, 5);
PHP
# Flush everything
wp rewrite flush
wp cache flush
echo "Routing fix applied!"
EOF

View file

@ -0,0 +1,77 @@
#!/bin/bash
echo "=== Fixing Template Recognition ==="
# SSH into staging and fix template loading
ssh roodev@146.190.76.204 << 'EOF'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html
# First, let's check what WordPress thinks the template should be
echo "Checking page template assignment..."
PAGE_ID=5334
TEMPLATE=$(wp post meta get $PAGE_ID _wp_page_template)
echo "Page $PAGE_ID has template: $TEMPLATE"
# Clear the template and reassign it
echo "Clearing and reassigning template..."
wp post meta delete $PAGE_ID _wp_page_template
wp post meta add $PAGE_ID _wp_page_template "page-trainer-registration.php"
# Also try setting it as default first, then back
wp post meta update $PAGE_ID _wp_page_template "default"
wp post meta update $PAGE_ID _wp_page_template "page-trainer-registration.php"
# Let's also check if we need to use a different template path
echo -e "\nTrying alternate template assignment..."
wp post meta update $PAGE_ID _wp_page_template "astra-child-hvac/page-trainer-registration.php"
# Check result
TEMPLATE_AFTER=$(wp post meta get $PAGE_ID _wp_page_template)
echo "Template after update: $TEMPLATE_AFTER"
# Clear all caches
wp cache flush
wp breeze purge --cache=all
wp rewrite flush
# Also update the child theme functions to better handle templates
echo -e "\nUpdating child theme template handling..."
cat > wp-content/themes/astra-child-hvac/functions-template-fix.php << 'PHP'
<?php
// Force template loading for HVAC pages
add_filter('template_include', function($template) {
if (is_page()) {
$page_slug = get_post_field('post_name', get_queried_object());
$page_path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
// Check if this is an HVAC page
if (strpos($page_path, 'trainer') !== false || strpos($page_path, 'master-trainer') !== false) {
// Map URLs to templates
$template_map = array(
'trainer/registration' => 'page-trainer-registration.php',
'trainer/dashboard' => 'page-trainer-dashboard.php',
'master-trainer/dashboard' => 'page-master-dashboard.php',
'training-login' => 'page-trainer-login.php',
);
foreach ($template_map as $path => $template_file) {
if (strpos($page_path, $path) !== false) {
$child_template = get_stylesheet_directory() . '/' . $template_file;
if (file_exists($child_template)) {
return $child_template;
}
}
}
}
}
return $template;
}, 99);
PHP
# Include the fix in functions.php
echo -e "\n// Include template fix" >> wp-content/themes/astra-child-hvac/functions.php
echo "require_once('functions-template-fix.php');" >> wp-content/themes/astra-child-hvac/functions.php
echo -e "\nTemplate fix applied!"
EOF