将外部PHP文件包括到Wordpress自定义模板中
-
-
看看http://codex.wordpress.org/Integrating_WordPress_with_Your_Web站点可以回答您的问题.Take a look at http://codex.wordpress.org/Integrating_WordPress_with_Your_Website should answer your question.
- 0
- 2013-08-26
- Mark Davidson
-
您可以使用它.你要面对什么问题?You can use it. What's the problem are you facing?
- 1
- 2013-08-26
- Vinod Dalvi
-
嗨,我只是将文件复制到wordpress文件夹中,并正确链接了它们..无论如何,这个问题的答案是什么?我只得到一张空白的白纸hi, i just copied my files inside the wordpress folder and linked them properly.. anyway, whats the answer on this question? I just get a blank white page
- 0
- 2013-08-26
- Jeremi Liwanag
-
为了清楚起见,您是否要包括位于外部站点上的文件?如果是这样,php文件站点的当前权限是否允许公众对这些文件进行读取访问?Just to be clear, you are trying to include files that are located on an external site? If so, do the current permissions of the php files site allow public read access to these files?
- 0
- 2014-02-13
- iyrin
-
6 个回答
- 投票数
-
- 2014-03-25
否,您不能按照指示的方式在主题中包含PHP文件.
要知道为什么,我们要做的就是行使一些常识和基本批判性思维:
- 您将向远程服务器执行远程请求,
include
无法做到 - 如果奇迹般行得通,那么您将拥有重大的安全漏洞,因为我可以执行以下操作:
include( 'http://yoursite/wp-config.php' ); echo DB_PASSWORD;
.想象一下,黑客网站多么容易!是什么阻止了我包括facebook.com/index.php
? - 如果即便如此,它仍然可以正常工作,并且很安全,请想象速度缓慢!您向服务器发出请求,然后服务器继续为另一个请求头,另一个为边栏,另一个为页脚,所有这些等待着,谁会被打扰?
- 但是最后,即使include可以发出远程请求.不能,为什么会返回PHP代码?它不会返回服务器吐出的内容,也就是最终的html吗?服务器会在返回任何PHP之前运行任何PHP,因此其中没有PHP.
因此,不可能.但是,如果将问题中的代码快速复制粘贴到PHP脚本中,然后再进行快速访问,就会发现比以前任何人都快.
相反,您只能包含服务器当前正在运行的本地文件系统中的文件.您永远不会将URL传递给include或require语句.
因此,如果我位于index.php中,并且同一文件夹中有一个名为sidebar.php的文件,我将这样包含它:
include('sidebar.php');
include
将返回一个值.include语句的语义是通用的基本PHP,并且在此Q& A网站上没有涉及.但是我让这个问题保持开放,因为您应该使用WordPress API函数,而不是包含并满足您的需求.
要在主题中包含模板部分,请使用:
get_template_part( 'partname' );
此功能还将在子主题或父主题中查找您指定的部分.
例如,在此代码中:
get_template_part( 'sidebar', 'first' );
它将寻找并包括发现的第一个:
- 子主题
sidebar-first.php
- 父主题
sidebar-first.php
- 子主题
sidebar.php
- 父主题
sidebar.php
应注意,您仅应使用get_template_part包含模板文件.请勿使用它来包含诸如库之类的文件或其他不输出html的PHP文件.
我建议您先阅读以下主题,然后再继续:
- PHP.Net上的include和require函数
- 父母和孩子主题
- 模板层次结构以及WordPress如何选择要使用的模板
-
<?php php ?> <?php tag ?> <?php spam ?> <?php and ?> <?php why ?> <?php its ?> <?php bad ?>
-
get_template_part
-
WP_HTTP
API,对于那些真正的时代,您确实需要对API进行远程请求.wp_remote_get
和wp_remote_post
之类的功能将在此处提供帮助. - PHP调试.您的白页是PHP致命错误,但是由于您设置了将错误记录从前端隐藏起来的错误日志记录,因此该错误日志文件已记录到您没有意识到的错误日志文件中.找到您的错误日志,在
WP_DEBUG
中定义wp-config.php
,并安装诸如调试栏或查询监视器之类的插件,这些将极大地帮助您.确保在模板页脚中调用wp_footer
.
No, you cannot include PHP files in a theme the way you indicated.
To see why, all we have to do is exercise some common sense and basic critical thinking:
- You would be doing a remote request to a remote server
include
cannot do this - If by some miracle it worked, you would have a major security exploit as I could then do something like this:
include( 'http://yoursite/wp-config.php' ); echo DB_PASSWORD;
. Imagine how easy it would be to hack websites! Whats to stop me includingfacebook.com/index.php
? - If even then it somehow worked, and it was secure, imagine the slowness! You make a request to the server, and then the server goes and makes another for the header, and another for the sidebar, and another for the footer, all that waiting, who could be bothered?
- But finally, even if include could make remote requests. Which it can't, why would it return PHP code? Won't it return what the server spits out, aka the finalised html? The server runs any PHP before it gets returned so there'd be no PHP in what your including..
So no, it's not possible. But a quick copy paste of the code in your question into a PHP script followed by a quick visit would have told you that quicker than anybody here.
Instead, you can only include files from the local file system your server is currently running on. You would never pass in a URL to an include or a require statement.
So if I'm in index.php, and there is a file in the same folder called sidebar.php, I would include it like this:
include('sidebar.php');
include
returns a value if this failed. The semantics of the include statement are general basic PHP and are off topic on this Q&A site.However I kept this question open because there is a WordPress API function you should be using instead that is better than include and require for what you need.
To include a template part in a theme, use:
get_template_part( 'partname' );
This function will also look inside a child or parent theme for the part you specified.
For example, in this code:
get_template_part( 'sidebar', 'first' );
It will look for and include the first it finds of these:
- child theme
sidebar-first.php
- parent theme
sidebar-first.php
- child theme
sidebar.php
- parent theme
sidebar.php
It should be noted, that you should only use get_template_part to include template files. Don't use it to include files such as libraries, or other PHP files that do not output html.
I suggest you read up on the following subjects before continuing:
- the include and require functions on PHP.Net
- parent and child themes
- the template hierarchy and how WordPress chooses which template to use
<?php php ?> <?php tag ?> <?php spam ?> <?php and ?> <?php why ?> <?php its ?> <?php bad ?>
get_template_part
- the
WP_HTTP
API, for those genuine times you really need to do a remote request to an API. Functions such aswp_remote_get
andwp_remote_post
will help here. - PHP Debugging. Your white pages are PHP Fatal errors, but because you have error logging set to hide it from the front end, it's being logged to an error log file you're unaware of. Find your error log, define
WP_DEBUG
in yourwp-config.php
and install a plugin such as debug bar or query monitor, these will help you enormously. Make sure you callwp_footer
in your templates footer though.
-
- 2014-02-13
假设您有这种情况:
您的wordpress安装位于站点的" www"根文件夹中,而header.php位于" www/includes"文件夹中.
在您的wordpress主题中,只需添加
&lt;?php include('includes/header.php'); ?>
当WordPress读取主题文件时,它使用根路径.
suppose you have this cenario:
Your wordpress instalation is hosted on 'www' root folder as your site, and your header.php is on 'www/includes' folder.
In you wordpress theme, just add
<?php include('includes/header.php'); ?>
When WordPress reads theme files, it uses the root path.
-
- 2014-02-03
您不能在
include()
函数上包含外部资源!通过服务器内部路径包含作品.您不能根据需要添加网址.这是PHP的安全问题!您可以使用:
get_file_contents('http://your.url');
或使用Curl库.
You cannot include an external resource on
include()
functions! Include works by server internal paths. You cannot include an url as you want. It's a PHP security problem!You can use:
get_file_contents('http://your.url');
or use a Curl library.
-
请在回答中添加大量解释,以帮助用户理解您的观点.Please add a good deal of explanation to your answers to help the user understand your point.
- 1
- 2014-02-03
- Maruti Mohanty
-
- 2017-01-12
当我检查核心文件时,以下是我找到的代码 在文件
wp/wp-includes/general-template.php
中用于函数get_header()
我发现以下几行
$templates[] = 'header.php'; locate_template( $templates, true );
所以我在这样的代码(
index.php
)中使用它<?php get_header(); $myphpfile[] = 'grid.php'; locate_template( $myphpfile, true ); ?>
When i checked the core files following is the code i found for in the file
wp/wp-includes/general-template.php
for functionget_header()
i found following lines
$templates[] = 'header.php'; locate_template( $templates, true );
So I am using it in my code (
index.php
) like this<?php get_header(); $myphpfile[] = 'grid.php'; locate_template( $myphpfile, true ); ?>
-
- 2018-04-24
对于儿童主题:
<?php echo get_stylesheet_directory_uri().'/subfoldername/add.php';?>
您可以使用
For Parent Theme: get_template_directory() or get_template_directory_uri() For Child Theme: get_stylesheet_directory() or get_stylesheet_directory_uri()
For Child Theme:
<?php echo get_stylesheet_directory_uri().'/subfoldername/add.php';?>
You can use
For Parent Theme: get_template_directory() or get_template_directory_uri() For Child Theme: get_stylesheet_directory() or get_stylesheet_directory_uri()
-
- 2020-05-04
在这样一个古老的问题上发帖的形式可能不佳,但是我最近不得不做类似的事情,并且在使用此处未显示的方法时发现了此结果.记下来供以后的探索者使用;).
我的用例是:
- Wordpress安装在子目录中
- 我想在父网站中添加页脚/页眉元素,而不是将wordpress用于这些元素
因为
include()
或require()
可以使用绝对服务器路径(例如:/var/www/vhosts/foo.bar/httpdocs/includes/baz.php
),我们可以使用Wordpress的ABSPATH
定义生成此代码:include( dirname( ABSPATH ) . '/includes/header.php' );
这将返回
/var/www/vhosts/foo.bar/httpdocs/includes/baz.php
.即使Wordpress位于子目录中(例如:
/var/www/vhosts/foo.bar/httpdocs/blog/
),dirname( ABSPATH )
也会为我们提供文档根目录.可能还有其他方法,例如使用
$_SERVER['DOCUMENT_ROOT']
超全局变量,但我没有对此进行探讨.Might be poor form to post on such an old question, but I recently had to do something similar and found this result while using a method not shown here. Jotting it down for future explorers ;).
My use case was:
- Wordpress installed in subdirectory
- I wanted to include footer/header elements from the parent site, rather than using wordpress for these elements
Because
include()
orrequire()
can use absolute server paths (eg:/var/www/vhosts/foo.bar/httpdocs/includes/baz.php
), we can use Wordpress'ABSPATH
definition to generate this:include( dirname( ABSPATH ) . '/includes/header.php' );
this will return
/var/www/vhosts/foo.bar/httpdocs/includes/baz.php
.Even when Wordpress is in a subdirectory (eg:
/var/www/vhosts/foo.bar/httpdocs/blog/
),dirname( ABSPATH )
gives us the document root regardless.There might be other ways to do this, using the
$_SERVER['DOCUMENT_ROOT']
superglobal for example, but I didn't explore that.
是否可以将外部PHP文件包含到自定义模板中?
我正在尝试将博客添加到我的网站中.我的网站中已经有页眉,页脚和侧栏布局.我可以在自定义模板中使用它们吗?
我也尝试过: