WordPress桌面/WebView重定向需求及wp-admin排除、报错问题求助
WordPress子主题重定向解决方案:避免后台误跳转+修复Header警告
Hey there! Let's work through your redirect issues together. I've helped several users fix exactly these two problems, so here's a tested approach that should resolve everything cleanly.
Core Issues to Fix
First, let's break down why you ran into those problems:
- 误重定向后台: Your previous code didn't exclude WordPress admin pages, so any desktop access to
/wp-admingot redirected too. - Headers Already Sent Warning: This happens when your code tries to send a redirect after WordPress has already started outputting HTML (like whitespace, or other code that echoes content). We need to hook into the right WordPress action to run the redirect before any output.
Step-by-Step Solution
Add this code to your child theme's functions.php file (make sure there are no extra spaces or blank lines before the opening <?php tag!):
<?php // 自定义重定向逻辑 add_action('template_redirect', 'custom_mobile_desktop_redirect'); function custom_mobile_desktop_redirect() { // 1. 排除后台页面和AJAX请求,避免误跳转 if (is_admin() || wp_doing_ajax()) { return; } // 配置参数:修改为你的实际值 $new_site_url = 'https://your-new-website.com'; // 新网站地址 $allowed_package = 'com.your.app.package'; // 允许访问的App包名(从App的User Agent里获取) // 2. 判断是否为桌面端访问 $is_desktop = !wp_is_mobile(); // 3. 判断是否是允许的App WebView访问 $is_allowed_app = false; if (isset($_SERVER['HTTP_USER_AGENT'])) { $user_agent = $_SERVER['HTTP_USER_AGENT']; // 检查User Agent是否包含指定包名(注意大小写,根据实际情况调整) if (strpos($user_agent, $allowed_package) !== false) { $is_allowed_app = true; } } // 4. 执行重定向逻辑 // 桌面端 或者 非允许的App访问 → 跳转到新网站 if ($is_desktop || !$is_allowed_app) { wp_redirect($new_site_url, 302); // 302临时重定向,如需永久重定向改为301 exit(); // 必须加上exit,终止后续代码执行 } }
Key Details Explained
template_redirectHook: This action runs right before WordPress loads the template, so no HTML has been output yet—this fixes the "headers already sent" warning completely.- Excluding Admin/AJAX: The
is_admin()check skips all/wp-adminpages, andwp_doing_ajax()ensures AJAX requests (used by WordPress for things like comments or admin actions) aren't redirected. - Desktop Detection:
!wp_is_mobile()uses WordPress's built-in function to identify desktop devices (note: some tablets might be classified as desktop here, but it's reliable for most cases). - App WebView Check: We look for your specific app package name in the User Agent string. Make sure you get the exact package name from your app developer (it's usually something like
com.yourcompany.yourapp).
Additional Tips to Avoid Issues
- Check for Whitespace: Make sure your
functions.phphas no blank lines or spaces before the opening<?phptag—this is a common cause of the headers warning. - Test with Cache Cleared: Browsers and caching plugins can cache redirects, so clear your browser cache and any WordPress cache (like WP Rocket, W3 Total Cache) after adding the code.
- Adjust Redirect Type: Use
302for temporary redirects (if you might switch back later) or301for permanent redirects (better for SEO if this is a permanent move). - Verify User Agent: If the app check isn't working, you can log the User Agent string to confirm what your app sends. Add this line temporarily inside the function (then remove it after testing):
You'll find the log inerror_log('User Agent: ' . $_SERVER['HTTP_USER_AGENT']);wp-content/debug.log(enable debug mode inwp-config.phpif needed).
内容的提问来源于stack exchange,提问作者TheBTCGuy
相关产品推荐
相关产品推荐

