|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Next.js Headless Theme |
| 4 | + * |
| 5 | + * Redirects all frontend requests to the Next.js application. |
| 6 | + * Allows admin, login, REST API, and other WordPress internals. |
| 7 | + */ |
| 8 | + |
| 9 | +// Redirect frontend requests to Next.js |
| 10 | +add_action('template_redirect', function () { |
| 11 | + // Allow WordPress admin area |
| 12 | + if (is_admin()) { |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + // Allow login/logout pages |
| 17 | + if (strpos($_SERVER['REQUEST_URI'], 'wp-login') !== false || |
| 18 | + strpos($_SERVER['REQUEST_URI'], 'wp-signup') !== false || |
| 19 | + strpos($_SERVER['REQUEST_URI'], 'wp-activate') !== false) { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + // Allow REST API |
| 24 | + if (strpos($_SERVER['REQUEST_URI'], 'wp-json') !== false || |
| 25 | + strpos($_SERVER['REQUEST_URI'], rest_get_url_prefix()) !== false) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + // Allow cron |
| 30 | + if (strpos($_SERVER['REQUEST_URI'], 'wp-cron') !== false) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // Allow AJAX requests |
| 35 | + if (defined('DOING_AJAX') && DOING_AJAX) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // Allow XML-RPC (if needed for some integrations) |
| 40 | + if (strpos($_SERVER['REQUEST_URI'], 'xmlrpc.php') !== false) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + // Get Next.js URL from environment |
| 45 | + $nextjs_url = getenv('NEXTJS_URL'); |
| 46 | + |
| 47 | + if ($nextjs_url) { |
| 48 | + // Redirect to Next.js with 301 (permanent redirect) |
| 49 | + wp_redirect($nextjs_url, 301); |
| 50 | + exit; |
| 51 | + } |
| 52 | +}); |
| 53 | + |
| 54 | +// Remove unnecessary frontend features for headless |
| 55 | +add_action('after_setup_theme', function () { |
| 56 | + // Remove emoji scripts |
| 57 | + remove_action('wp_head', 'print_emoji_detection_script', 7); |
| 58 | + remove_action('wp_print_styles', 'print_emoji_styles'); |
| 59 | + |
| 60 | + // Remove feed links |
| 61 | + remove_action('wp_head', 'feed_links', 2); |
| 62 | + remove_action('wp_head', 'feed_links_extra', 3); |
| 63 | + |
| 64 | + // Remove RSD link |
| 65 | + remove_action('wp_head', 'rsd_link'); |
| 66 | + |
| 67 | + // Remove wlwmanifest link |
| 68 | + remove_action('wp_head', 'wlwmanifest_link'); |
| 69 | + |
| 70 | + // Remove WordPress version |
| 71 | + remove_action('wp_head', 'wp_generator'); |
| 72 | +}); |
0 commit comments