这包括声明有什么问题?

时间:2014-05-12 21:09:43

标签: php wordpress include

此函数返回文件的内容,而不是fetch_link_settings_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');

}

1 个答案:

答案 0 :(得分:0)

你写这个:

include( plugins_url()."/plugin-child/plugin_overides.php");

为什么plugins_url()在那里? include函数严格基于文件系统:

The `include` statement includes and evaluates the specified file.

正如WordPress文档中所述,plugins_url()将为您提供完整的网址,该网址与安装WordPress的文件系统完全不同:

  

检索插件目录的绝对URL(不带   尾部斜杠)或者,当使用$ 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';作为   在包含文件中的第一个语句,它在   功能使用include。

道歉。函数,类,字符串,常量......几乎任何你想成为原始的东西,未处理的PHP都不会通过http://https:// URL传递,因为Apache将解析PHP指令&只需返回该文件的输出,而不是该文件中PHP的原始未处理内容

此外,原始海报的内容如下:

  

你无法帮助我,因为你所说的没有意义或者   你没有充分地解释自己。看看这些例子:

include realpath(dirname(FILE) . "/" . "relative_path");
include("data://text/plain;base64,".base64_encode($content));
include("data://text/plain,".urlencode($content));
     

全部来自the official PHP documentation。他们都使用   函数返回与其余部分连接的组件   网址。我也尝试过明确地输入文件路径和   结果是一样的。

引用的例子如下:

include realpath(dirname(FILE) . "/" . "relative_path");

这是一个文件系统级别包含,这是PHP文件包含在其他文件中的最常见方式。

include("data://text/plain;base64,".base64_encode($content));
include("data://text/plain,".urlencode($content));

这些都是data个网址。不是httphttps。因此,当您使用plugins_url()时,您获得的是一个完整的http://https:// URL,其中Apache解析PHP指令&只需返回该文件的输出,而不是该文件中PHP的原始未处理内容。或者非常清楚地解释in the PHP documentation you are linking to;强调我的:

  

如果在PHP中启用了“URL include wrappers”,则可以指定该文件   使用URL包含(通过HTTP或其他支持的包装器 - 请参阅   支持协议列表的协议和包装器)而不是   本地路径名。如果目标服务器将目标文件解释为PHP   代码,变量可以使用URL请求传递给包含的文件   与HTTP GET一起使用的字符串。 严格来说,这并不严格   包含文件并让它继承父文件的东西   变量范围;该脚本实际上是在远程服务器上运行的   然后将结果包含在本地脚本中。

回到你的例子,你现在说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。这意味着从该调用中获得的绝对ONLY输出是纯文本。没别了。

现在我看到你确实发布了plugin_overides.php的内容。我上面的例子解释仍然适用,但仍然是一个基本问题。这是你的功能;只是界面&amp;例如return

function fetch_link_settings_override(){

    // Other code removed. Just a structural illustration for now.
    return compact('destination_url', 'target', 'disable_links');

}

fetch_link_settings_override()运行时,您是否实际呼叫plugin_overides.php?好吧,如果该功能没有运行,那么100%无法获得任何输出。但假设有诚意,请在此处查看您的return声明:

return compact('destination_url', 'target', 'disable_links');

如果您要返回compact,那么您将返回一个数组。您不能简单地将裸阵列作为此http://some.cool.website/plugin-child/plugin_overides.php之类的URL调用返回。输出最多只是单词Array

如果目标是采取该阵列&amp;做某事,然后你应该在fetch_link_settings_override中使用json_encode,然后在接收方使用json_decode。所以return语句就是这样的:

return json_encode(compact('destination_url', 'target', 'disable_links'));
相关问题