首页 > 解决方案 > 使用 WordPress 插件中的目录在 URL 上生成自定义输出

问题描述

我正在尝试创建一个自定义 URL,我将在其中输出一些字符串。问题是我希望这个 URL 包含一个/.well-known/目录,当我这样做时,会发生 403 禁止错误。所以我当前的代码在没有目录的情况下工作,而不是在/.well-known/目录下工作。这是我的代码:

add_action('parse_request', 'custom_output');
function custom_output() {            
    global $wp;
    global $wp_query;
    $urlEnd = 'custom-output'; // This will work
    $desiredUrlEnd = '.well-known/custom-output'; // This will not work
    
    if (!$wp_query->is_main_query()) {
        return;
    }

    if ($wp->request === $desiredUrlEnd) {
        $wp_query->set($desiredUrlEnd, 1);
    }

    if ($wp_query->get($desiredUrlEnd)) {
        @ini_set('display_errors', 0);
        @header('Cache-Control: no-cache');
        @header('X-Robots-Tag: noindex, follow');

        echo 'custom output';
        exit;
    }
}

所以它适用于$urlEnd输出显示,https://example.com/custom-output但它不适用,$desiredUrlEnd并且在我访问时会导致错误https://example.com/.well-known/custom-output。我怎样才能让它工作?

谢谢!

标签: phpwordpresscustom-wordpress-pages

解决方案


推荐阅读