Php脚本作为页面模板进入WordPress

时间:2017-02-19 18:51:53

标签: php wordpress

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

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

  

警告:require_once(application / config / routes.php):无法打开   stream:没有这样的文件或目录   /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();
?>

感谢任何帮助。谢谢。

编辑: router.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://在服务器中禁用了包装器   配置by allow_url_include = 0 in   /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):   无法打开流:没有找到合适的包装器   /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')in   /opt/lampp/htdocs/wordpress/wordpress/wp-content/themes/twentyseventeen/core/router.php   在第55行

1 个答案:

答案 0 :(得分:0)

这似乎是路径问题,您在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结构的原因,尽管如果您更改{在allow_url_include文件中{1}}到1,但您在实时服务器上会做什么?要解决此问题,请在您的要求中添加php.ini,而不是像下面的&amp;看看是否有效:

../

中执行此操作

router.php