wp_redirect不会保存功能.php具体取决于位置



我在函数中添加了一些代码.php(在wp引擎中托管的wordpress主题编辑器中(,根据我放置wp_redirect的位置,它将保存或不保存

示例:当我这样做时它会保存

add_action('template_redirect','test_template');
//this one saves fine
function test_template() {
    global $wp_query;
    $userId = $wp_query->get( 'userId', NULL );
    $url = get_site_url();
    if ( NULL !== $userId ) {
        wp_redirect($url);
        exit;
    }
}
//However, when I do this I get: "Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP."
function test_template() {
    global $wp_query;
    $userId = $wp_query->get( 'userId', NULL );
    $url = get_site_url();
    wp_redirect($url);
    exit;
}

不知道为什么第二个功能不会保存,有什么想法吗?

第二个是触发无限重定向循环。根据WordPress Codex: https://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect

此操作钩子在WordPress确定要加载的模板页面之前执行

这意味着它将在每次加载页面模板之前运行。所以基本上在wp_redirect($url(之后,动作template_redirect将被钩住并再次重定向。

最新更新