【问题标题】:Php script into WordPress as page template将 PHP 脚本导入 WordPress 作为页面模板
【发布时间】:2017-07-08 22:54:06
【问题描述】:

我正在尝试将购买的 php 脚本 (yougrabber) 作为页面模板集成到 WordPress 中。

脚本可以作为独立网站正常工作。但是当我尝试将其作为页面模板插入并打开时,我收到以下错误:

警告:require_once(application/config/routes.php): 未能打开 流:中没有这样的文件或目录 /opt/lampp/htdocs/wordpress/wordpress/wp-content/themes/twentyseventeen/yougrabber/core/router.php 第 55 行

致命错误:require_once():需要打开失败 'application/config/routes.php' (include_path='.:/opt/lampp/lib/php') 在 /opt/lampp/htdocs/wordpress/wordpress/wp-content/themes/twentyseventeen/yougrabber/core/router.php 第 55 行

我认为脚本应该加载到根目录中。

这就是我将脚本 index.php 放入主题目录并告诉 wp 它是页面模板时的样子。

<?php
/*
Template Name: TESTIRANJE
*/
?>
<?php
//error_reporting(E_ALL); //uncomment for development server

define("BASE_PATH", str_replace("\\", NULL, dirname($_SERVER["SCRIPT_NAME"]) == "/" ? NULL : dirname($_SERVER["SCRIPT_NAME"])) . '/');

require_once "core/router.php";

Router::init();

function __autoload($class) { Router::autoload($class); }

Router::route();
?>

感谢任何帮助。谢谢。

编辑: 路由器.php

<?php
class Router
{
    private static $current_page;
    private static $page_struct;
    private static $is_router_loaded;

    private function __construct()
    {
        self::$current_page = self::getPage();
        self::$page_struct = explode("/", self::$current_page);
        self::$page_struct = array_values(array_filter(self::$page_struct, 'strlen'));
    }

    public static function init()
    {
        if(self::$is_router_loaded instanceof Router) return self::$is_router_loaded;

        else return self::$is_router_loaded = new Router;
    }

    public static function route()
    {
        self::loadAutoload();

        if(!self::getController())
        {
            try {
                self::loadDefaultPage();
            }
            catch(ConfigException $e) { return self::error404(); }
        }

        self::loadRewrites();
        self::loadPostedPage();
    }

    private static function loadDefaultPage()
    {
        require('application/config/routes.php');

        if(!is_array($routes["default_page"])) throw new ConfigException();

        else
        {
            $controller_cname = 'application\\Controllers\\'.$routes['default_page'][0];
            $controller_obj = new $controller_cname;
            $controller_obj->$routes['default_page'][1]();
        }
        exit;
    }

    private static function loadAutoload()
    {
        require_once("application/config/routes.php");
        require_once("/application/config/autoload.php");

        $loader =\core\Loader::getInstance(true);

        foreach($autoload['libraries'] as $library)
        {
            $loader->library($library);
        }
        foreach(array_unique($autoload['controllers']) as $controller)
        {
           if((strtolower(self::getController()) != strtolower($controller)) && (self::getController() != null && $routes['default_page'][0] == 'users')) $loader->controller($controller);
        }
    }

    private static function loadRewrites()
    {
        require("application/config/routes.php");

        foreach ($routes as $rewrittenPage => $realPage)
        {
            if(is_array($realPage)) continue;

            if($rewrittenPage == str_replace(BASE_PATH, NULL, $_SERVER["REQUEST_URI"])) self::setPage($realPage);

            else if(preg_match_all('#\[(.*)\]#U', $rewrittenPage, $param_names))
            {
                $getRegex = preg_replace("#\[.*\]#U", "(.*)", $rewrittenPage);
                preg_match_all("#^\/?".$getRegex."$#", self::$current_page, $param_values); unset($param_values[0]);
                if(in_array(null, $param_values)) continue;

                else
                {
                    $i = 0;
                    foreach($param_values as $p_value)
                    {
                        $realPage = str_replace('['.$param_names[1][$i].']', $param_names[1][$i].':'.$p_value[0], $realPage);
                        $i++;
                    }
                    self::setPage($realPage);
                }
            }
        }
    }

    private static function loadPostedPage()
    {
        if(self::getController() != null && $controller = self::getController())
        {
            $controller = "application\\Controllers\\".$controller;
            $controller = new $controller;

            if(!self::getMethod())
            {
                if(method_exists($controller, 'index'))
                {
                    $controller->index();
                }
            }
            else
            {
                $method = self::getMethod();

                if(!method_exists($controller, $method)) return self::error404();

                $method_data = new ReflectionMethod($controller, $method);

                if($method_data->isPublic() == true)
                {
                    if(!self::getParameters())
                    {
                        if($method_data->getNumberOfRequiredParameters() == 0) $controller->$method(); else self::error404();
                    }
                    else
                    {
                        $parametersToSet = self::getParameters();
                        $sortParams = array();

                        foreach($method_data->getParameters() as $params)
                        {
                            if(!$params->isOptional() && !isset($parametersToSet[$params->getName()])) return self::error404 ();

                            if($params->isOptional() && !isset($parametersToSet[$params->getName()])) $sortParams[] = $params->getDefaultValue();
                            else $sortParams[] = $parametersToSet[$params->getName()];
                        }
                        $method_data->invokeArgs($controller, $sortParams);
                    }
                } else return self::error404();
            }
        }
    }

    public static function error404()
    {
        header("HTTP/1.0 404 Not Found");
        die(file_get_contents('application/errors/404.html'));
        exit;
    }

    public static function autoload($className)
    {
        if(class_exists($className)) return true;

        else
        {
            $className = strtolower(str_replace('\\', '/', $className));
            if(!file_exists($className.'.php')) return self::error404();;

            require_once $className .'.php';
        }
    }

    private static function getController()
    {
        if(isset(self::$page_struct[0]))
            return self::$page_struct[0];
    }

    private static function getMethod()
    {
        if(isset(self::$page_struct[1]))
            return self::$page_struct[1];
    }

    private static function getParameters()
    {
        $parameters = array();

        foreach(self::$page_struct as $place => $args)
        {
            if($place > 1 && strstr($args, ':'))
            {
                $parameter = explode(":", $args);
                $parameters[$parameter[0]] = $parameter[1];
                $_GET[$parameter[0]] = $parameter[1];
            }
        }

        return $parameters;
    }

    public static function getPage()
    {
        $rpath = BASE_PATH == "/" ? NULL : BASE_PATH;

        if(self::$current_page == null) self::$current_page = (
                 str_replace('?'.$_SERVER["QUERY_STRING"], NULL,
                         str_replace($rpath, NULL, $_SERVER["REQUEST_URI"])));

        return self::$current_page;
    }

    private static function setPage($page)
    {
        self::$current_page = $page;
        self::$page_struct = explode("/", self::$current_page);
        self::$page_struct = array_filter(self::$page_struct, 'strlen');
    }
}
class ConfigException Extends Exception {}
?>

在尝试了@ArsalanMithani 的建议后,现在得到:

警告:require_once(): http:// 包装器在服务器中被禁用 通过 allow_url_include=0 配置 /opt/lampp/htdocs/wordpress/wordpress/wp-content/themes/twentyseventeen/core/router.php 第 55 行

警告: 需要一次(http://localhost/wordpress/wordpress/wp-content/themes/twentyseventeen/application/config/routes.php): 无法打开流:找不到合适的包装器 /opt/lampp/htdocs/wordpress/wordpress/wp-content/themes/twentyseventeen/core/router.php 第 55 行

致命错误:require_once():需要打开失败 'http://localhost/wordpress/wordpress/wp-content/themes/twentyseventeen/application/config/routes.php' (include_path='.:/opt/lampp/lib/php') 在 /opt/lampp/htdocs/wordpress/wordpress/wp-content/themes/twentyseventeen/core/router.php 第 55 行

【问题讨论】:

  • 你的目录结构是什么?
  • @ArsalanMithani 不确定天气我理解这个问题。我正在使用linux(fedora)。在上面安装了xampp...
  • 我要你的目录结构,你的文件应该被正确放置,wp-content/theme/yourappfolder 然后把你的模板文件放在你的主题根上。一样吗?
  • index.php(问题中的代码)是我购买的脚本。我在其中添加了前五行(告诉 wp 这是一个模板文件)。将整个脚本放在themes/twentyseventeen/yougrabber(php script) yourgrabber 文件夹包含:index.php 和文件夹:应用程序、核心、文件。
  • 现在我将 index.php(因为在主题/twentyseventeen/ 文件夹中已经有一个 index.php)重命名为 templateindex.php 并将其移至主题/twentyseventeen/。现在显示错误: 警告:require_once(core/router.php): failed to open stream: No such file or directory in /opt/lampp/htdocs/wordpress/wordpress/wp-content/themes/twentyseventeen/templateindex.php 在线11 致命错误:require_once():无法在 /opt/lampp/htdocs/wordpress/wordpress/wp-content/ 中打开所需的 'core/router.php' (include_path='.:/opt/lampp/lib/php')主题/twentyseventeen/templateindex.php 第 11 行

标签: php wordpress


【解决方案1】:

这似乎是路径问题,您在 WordPress 中使用它,因此您必须以 WordPress 方式定义相关路径。

使用get_template_directory_uri() 获取主题路径,如果您使用的是子主题,然后使用get_stylesheet_directory_uri()

require( get_template_directory_uri() . '/directorypathtoscriptfiles');

如果你把你的脚本写成wp-content/yourtheme/yourscriptdir,那么你的路径应该是:

require( get_template_directory_uri() . '/yourscriptdir/core/router.php');

编辑

生成警告是因为您包含的文件已使用完整的 URL,您可能会收到一些 HTML,这就是我要求您的 dir 结构的原因,尽管如果您更改 @ 它将起作用987654326@ 到 1php.ini 文件中,但是你会在实时服务器上做什么?为了解决这个问题,现在在您的要求中添加../,而不是像下面这样的完整网址,看看是否有效:

router.php 中执行此操作

require( '../application/config/routes.php');

【讨论】:

  • 我在哪里添加它?哪个文件?
  • 替换成上面的要求,确保路径正确。
  • 不,仍然没有改变错误。问题是它现在显示文件的正确路径但仍然无法正常工作。
猜你喜欢
  • 2016-05-02
  • 2012-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-11
  • 2017-11-09
  • 1970-01-01
  • 2014-05-25
相关资源
最近更新 更多