[英]What's wrong with this include statement?
此函数返回文件的内容,而不是文件中fetch_link_settings_overide()
的结果。
问题不在于overide函数,因为在最初的错误之后,我注释掉了我的修改,以确保它不是我在那里所做的。
function fetch_link_settings(){
include( plugins_url()."/plugin-child/plugin_overrides.php");
return fetch_link_settings_override();
}
添加派生函数plugin-child / plugin_overrides.php的内容,因为我们目前无法到达任何地方。
function fetch_link_settings_override(){
global $post;
// If the destination url is set by the user, use that. Otherwise, use the permalink
$destination_url = get_post_meta($post->ID, '_promo_slider_url', true);
// ASAdd additional place to look in the case of the post being via the PODS advert track
if( ! $destination_url )
$destination_url = get_post_meta($post->ID, 'okd_advert_link', true);
if( ! $destination_url )
$destination_url = get_permalink($post->ID);
// If the target attribute is set by the user, use that. Otherwise, set it to _self
$target = get_post_meta($post->ID, '_promo_slider_target', true);
if( ! $target ) $target = '_self';
// Setup the disable links variable
$disable_links = get_post_meta($post->ID, '_promo_slider_disable_links', true);
return compact('destination_url', 'target', 'disable_links');
}
你这样写:
include( plugins_url()."/plugin-child/plugin_overides.php");
为什么那里有plugins_url()
? include
功能严格基于文件系统:
The `include` statement includes and evaluates the specified file.
如WordPress文档中所述, plugins_url()
将为您提供完整的Web URL,该URL与WordPress安装在其上的文件系统有100%的差异:
检索绝对URL到plugins目录(不带斜杠),或者在使用$ path参数时检索到该目录下的特定文件。
所以也许应该是这样的:
include("/plugin-child/plugin_overides.php");
还是您需要plugin_dir_path()
?
include(plugin_dir_path( __FILE__ ) . "/plugin-child/plugin_overides.php");
但这似乎是错误的。 /plugin-child/plugin_overides.php
在哪里? 尝试这样做:
include("/full/path/to/wordpress/and/this/plugin-child/plugin_overides.php");
只需将/full/path/to/wordpress/and/this/
替换为/plugin-child/plugin_overides.php
的实际文件系统路径。
编辑:由于尽管所有其他建议,原始海报仍然坚持使用plugins_url()
,所以这是我的详细回复:
…您说“您不能通过带有include的URL加载原始函数”很好,这无关紧要,因为即使我添加$ some_var ='smith'; 作为包含文件中的第一条语句,使用包含在函数中不可见。
道歉。 函数,类,字符串,常量...几乎所有您想要原始的,未经处理的PHP都不会直接通过http://
或https://
URL传递,因为Apache会解析PHP指令并仅返回PHP的输出该文件,而不是该文件中未经处理的PHP原始内容 。
此外,原始海报还包含以下内容:
您不能帮我,因为您所说的话没有道理或您没有充分解释自己。 看下面的例子:
include realpath(dirname(FILE) . "/" . "relative_path"); include("data://text/plain;base64,".base64_encode($content)); include("data://text/plain,".urlencode($content));
全部取自官方PHP文档 。 它们都使用函数返回与URL其余部分串联的组件。 我也尝试过显式键入文件路径,结果是相同的。
引用的示例如下:
include realpath(dirname(FILE) . "/" . "relative_path");
这是文件系统级别的包含,这是将PHP文件包含到其他文件中的最常见方式。
include("data://text/plain;base64,".base64_encode($content));
include("data://text/plain,".urlencode($content));
这些都是data
URL。 不是http
或https
。 因此,再次使用plugins_url()
,您获得的是完整的http://
或https://
URL,Apache在其中解析PHP指令并仅返回该文件的输出 ,而不是PHP的未经处理的原始内容在那个文件中。 或正如您链接到的PHP文档中清楚解释的 ; 我的重点:
如果在PHP中启用了“ URL包含包装器”,则可以使用URL(通过HTTP或其他受支持的包装器-请参见受支持的协议和包装器以获取协议列表)而不是本地路径名来指定要包含的文件。 如果目标服务器将目标文件解释为PHP代码,则可以使用与HTTP GET一起使用的URL请求字符串将变量传递到包含的文件。 严格来讲,这与包括文件并让其继承父文件的变量作用域不同; 该脚本实际上是在远程服务器上运行的,然后将结果包含在本地脚本中。
回到您的示例,您现在说plugin_overides.php
的内容是$some_var = 'smith';
。 到底如何 如果是这样的PHP文件:
<?php
$some_var = 'smith';
?>
当您通过以下代码生成的URL调用该文件时:
include(plugins_url() . "/plugin-child/plugin_overrides.php");
假设您的网站是http://some.cool.website/
,那么您基本上是在拨打这样的电话:
http://some.cool.website/plugin-child/plugin_overides.php
因此, plugin_overides.php
的输出将为100%空白。 如果要获取该文件的输出,可以执行以下操作:
<?php
$some_var = 'smith';
echo $some_var;
?>
那将使smith
回归。 表示从该调用中获得的绝对唯一输出是纯文本。 没有其他的。
现在,我看到您实际上已经发布了plugin_overides.php
的内容。 我上面的示例解释仍然很恰当,但仍然是一个基本问题。 这是你的职责; 仅界面和return
例如:
function fetch_link_settings_override(){
// Other code removed. Just a structural illustration for now.
return compact('destination_url', 'target', 'disable_links');
}
运行时,您实际上在plugin_overides.php
调用fetch_link_settings_override()
吗? 好吧,如果该功能未运行,那么您将不可能100%获得任何输出。 但是假设您是出于诚信,请在此处查看您的return
声明:
return compact('destination_url', 'target', 'disable_links');
如果您返回compact
,那么您将返回一个数组。 您不能像http://some.cool.website/plugin-child/plugin_overides.php
这样简单地将裸数组作为URL调用返回。 输出最多仅是单词Array
。
如果目标是获取该数组并执行某项操作,则应在fetch_link_settings_override
使用json_encode
,然后在接收端使用json_decode
。 所以return
语句将是这样的:
return json_encode(compact('destination_url', 'target', 'disable_links'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.