What's wrong with this include statement?

2019-09-02 06:14发布

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

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();
}

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');

}

1条回答
在下西门庆
2楼-- · 2019-09-02 06:30

You write this:

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

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

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

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:

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");

Or perhaps you need the plugin_dir_path()?

include(plugin_dir_path( __FILE__ ) . "/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");

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

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

…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.

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));

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.

The examples cited are as follows:

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

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));

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:

If "URL include wrappers" are enabled in PHP, you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Supported Protocols and Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.

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';
?>

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

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

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

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;
?>

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

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');

}

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');

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.

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'));
查看更多
登录 后发表回答