这包括声明有什么问题? [英] What's wrong with this include statement?

查看:130
本文介绍了这包括声明有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此函数返回文件的内容,而不是 fetch_link_settings_overide()的结果。

This function is returning the content of the file rather the result of fetch_link_settings_overide() within it.

问题不在于overover函数,因为在初始错误之后我注释掉了我的修改,只是为了确保它不是我在那里做过的事情。

The issue is not with the overide function as after the initial error I commented out my modification just to be sure it wasn't something I had done there.

function fetch_link_settings(){
    include( plugins_url()."/plugin-child/plugin_overrides.php");
    return fetch_link_settings_override();
}

我们正在添加派生函数plugin-child / plugin_overrides.php的内容目前无处可去。

Adding the content of the derived function plugin-child/plugin_overrides.php as we are not getting anywhere currently.

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 函数严格基于文件系统:

Why is plugins_url() there? The include function is strictly based on the file system:

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

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

As explained in the WordPress docs, the plugins_url() would give you the full web URL which is 100% different than the file system WordPress is installed on:


检索绝对网址到plugins目录(没有
尾部斜杠),或者,当使用$ path参数时,到该目录下的特定文件

Retrieves the absolute URL to the plugins directory (without the trailing slash) or, when using the $path argument, to a specific file under that directory.

所以也许它应该是这样的:

So perhaps it should be like this:

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

或许您需要 plugin_dir_path()

Or perhaps you need the plugin_dir_path()?

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

但这似乎不对。 /plugin-child/plugin_overides.php 在哪里?尝试这样做:

But that seems wrong. Where would /plugin-child/plugin_overides.php? Try doing this:

include("/full/path/to/wordpress/and/this/plugin-child/plugin_overides.php");

只需将 / full / path /替换为/ wordpress /和/ this / ,实际文件系统路径为 /plugin-child/plugin_overides.php

Just replace /full/path/to/wordpress/and/this/ with the actual file system path to /plugin-child/plugin_overides.php.

编辑:由于原始海报持续使用 plugins_url()尽管提出了所有建议,但这是我的详细回复:

Since the original poster is persistent in using plugins_url() despite all of the suggestions otherwise, here is my detailed response:


...你说你不能通过带有包含的URL加载原始函数好吧
这是不相关的,因为即使我添加$ some_var ='史密斯';作为包含文件中的
第一个语句,它在使用include的
函数中不可见。

…you said "you cannot load raw functions via a URL with include" well this is not relevant because even if I add $some_var = 'smith'; as the first statement in the included file, it is not visible in the function using the include.

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

Apologies. Functions, classes, strings, constants… Just about anything that you want to be raw, unprocessed PHP will simply not be passed via an http:// or https:// URL because Apache will parse the PHP instructions & simply return the output of that file and not the raw, unprocessed contents of the PHP in that file.

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

Additionally the original poster contents the following:


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

You can’t help me because what you are saying does not make sense or you are not explaining yourself adequately. Look at these examples:

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

所有取自官方PHP文档。它们都使用
函数返回与
其余部分连接的组件。我也尝试过明确键入文件路径,
结果是相同的。

All taken from the official PHP documentation. They all use functions returning components that are concatenated with the rest of the url. I also tried this typing the filepath explicitly and the result is the same.

引用的例子如下:

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

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

This is a filesystem level include which is the most common way PHP files are included into other files.

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

这些都是数据网址。不是 http https 。所以当你使用 plugins_url()时,你得到的是一个完整的 http:// https:// Apache解析PHP指令的URL&只需返回该文件的输出,而不是该文件中PHP的原始未处理内容。或者在您链接到的PHP文档中非常清楚地解释;强调我的:

These are both data URLs. Not http or https. So again when you use plugins_url() what you are getting is a full http:// or https:// URL in which Apache parses the PHP instructions & simply return the output of that file and not the raw, unprocessed contents of the PHP in that file. Or as very clearly explained in the PHP documentation you are linking to; emphasis mine:


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

回到你的例子,你现在说 plugin_overides.php 的内容是 $ some_var ='smith'; 。究竟怎么样?如果它是这样的PHP文件:

Going back to your example, you say now the contents of plugin_overides.php is $some_var = 'smith';. How exactly? If it is a PHP file like this:

<?php
  $some_var = 'smith';
?>

当您通过以下代码生成的URL调用该文件时:

When you call that file via a URL generated by the following code:

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

假设您的网站 http://some.cool.website/ 您基本上是这样打来的电话:

Assuming your website is http://some.cool.website/ the you are basically making a call like this:

http://some.cool.website/plugin-child/plugin_overides.php

所以输出 plugin_overides.php 将100%空白。如果您想获得该文件的输出,可以执行以下操作:

So the output of plugin_overides.php would be 100% blank. If you wanted to get output of that file, you could do the following:

<?php
  $some_var = 'smith';
  echo $some_var;
?>

这将返回 smith 。这意味着从该调用中获得的绝对ONLY输出是纯文本。没有别的。

And that would return smith. Meaning the absolute ONLY output you would get from that call is pure text. Nothing else.

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

Now I see you actually have posted the contents of plugin_overides.php. My example explanation above is still apt, but still a basic question. This is your function; just the interface & return for example:

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 语句:

Do you actually call fetch_link_settings_override() in plugin_overides.php when it runs? Well, if that function does not run, then there is 100% no way you will ever get any output. But assuming good faith, look at your return statement here:

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

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

If you are returning compact, then you are returning an array. You cannot simply return a bare array as a URL call like this http://some.cool.website/plugin-child/plugin_overides.php. The output at most would be simply the word Array.

如果目标是采用该阵列&做某事,那么你应该使用 json_encode fetch_link_settings_override 中,然后使用 json_decode 。所以 return 语句将是这样的:

If the goal is to take that array & do something, then you should use json_encode in fetch_link_settings_override and then use json_decode on the receiving side of that. So the return statement would be something like this:

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

这篇关于这包括声明有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆