【问题标题】:How to directly access php file in WordPress如何在WordPress中直接访问php文件
【发布时间】:2015-12-01 17:49:59
【问题描述】:

我有一个生成 js 代码的 php 文件。我需要像 host/nameofile?args 一样访问这个文件

现在我以这种方式解决了这个问题 - 将我的 php 命名为 page-{slug}.php,在 Wordpress 中使用相同的 slug 创建了一个页面,并将此文件放在我当前的主题文件夹中。

我们的服务器在 nginx 上工作,只有一个入口点 index.php,因此它会阻止任何其他 php 查询。

有没有其他实现,不像我现在的?

【问题讨论】:

    标签: php wordpress nginx


    【解决方案1】:

    @parham 的解决方案是我正在寻找的)

    add_action( 'template_redirect', 'wpse131387_template_redirect' );
    function wpse131387_template_redirect( ){
        if ($_SERVER['REQUEST_URI'] == '/some-template') {
            global $wp_query;
            $wp_query->is_404 = false;
            status_header(200);
            include(dirname(__FILE__) . '/some-template.php');
            exit();
        }
    }
    

    【讨论】:

    • 最好说if (strpos($_SERVER['REQUEST_URI'], '/some-template')==0) 因为可能有args
    【解决方案2】:

    如何处理 php 文件中的所有代码并将它们添加到带有参数的函数(在 functions.php 中),该函数将返回 js 代码。这样你就可以从页眉/页脚调用函数,它会输出相关的js代码。

    【讨论】:

    • 看起来不错,但是有了这样的解决方案,我就失去了像<script src="{host}/{filename}?{args}"></script> 这样调用我的代码的可能性,我需要这样调用(
    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    相关资源
    最近更新 更多