返回WordPress插件的原始图像代理

时间:2018-09-06 作者:Richard Neumann

我正在尝试向wordpress插件添加一个文件,以使用插件配置中存储的身份验证令牌从远程API返回图像。

这是我当前的代码:

<?php
// Make sure we don\'t expose any info if called directly.
if (! function_exists(\'add_action\')) {
    echo \'Hi there!  I\\\'m just a plugin, not much I can do when called directly.\';
    exit;
}


function hinews_get_image($id, $mimetype) {
    $options = get_option(\'homeinfo_news_options\');
    $parm_token = \'?access_token=\' . $options[\'token\'];
    $base_url = \'https://myurl.com/\';
    $image_url = $base_url . $id . $parm_token;
    header(\'Content-type: \' . $mimetype);
    $image = file_get_contents($articles_url);

    if ($image === FALSE) {
        return \'Could not retrieve image.\';
    }

    return $image;
}


hinews_get_image($_GET[\'id\'], $_GET[\'mimetype\']);
?>
此文件,当前调用images.php 在我的插件文件夹中,当通过URL直接引用时,当然会返回纾困消息,因为它不是在wordpress上下文中调用的。我找不到任何有关如何从短代码呈现页面中正确调用此文件的信息。

调用代码为:

<?php
/**
* Plugin Name: HOMEINFO News
* Plugin URI: https://www.homeinfo.de/
* Description: News articles provided by HOMEINFO.
* Version: 0.0.1
**/

// Make sure we don\'t expose any info if called directly.
if (! function_exists(\'add_action\')) {
    echo \'Hi there!  I\\\'m just a plugin, not much I can do when called directly.\';
    exit;
}


include("settings.php");


add_shortcode(\'hinews\', \'hinews_shortcode\');


function hinews_shortcode(){
    wp_enqueue_style(\'hinews.css\', plugins_url(\'hinews.css\', __FILE__));
    wp_enqueue_script(\'hinews.js\', plugins_url(\'hinews.js\', __FILE__));
    $options = get_option(\'homeinfo_news_options\');
    wp_localize_script(\'hinews.js\', \'php_vars\', $options);
    $parm_token = \'?access_token=\' . $options[\'token\'];
    $base_url = \'https://myurl.com\';
    $articles_url = $base_url . $parm_token;
    $response = file_get_contents($articles_url);

    if ($response === FALSE) {
        return \'Could not load data from API. Check your credentials.\';
    }

    $news_list = json_decode($response);
    $result = \'\';

    foreach ($news_list as $news) {
        $result .= \'<h2>\' . $news->title . \'</h2>\';
        $result .= \'<p>\' . $news->text . \'</p>\';
        $result .= \'<br/>\';

        foreach ($news->images as $image) {
            $php_file = \'images.php\';
            $args = array(\'id\' => $image->id, \'mimetype\' => $image->mimetype);
            $url_args = \'?\' . http_build_query($args);
            $image_url = plugins_url($php_file . $url_args, __FILE__);      # What to do here?
            $result .= \'<img src="\' . $image_url . \'" alt="\' . $image->source . \'">\';
            $result .= \'<br/>\';
        }
    }

    return $result;
}
?>

1 个回复
最合适的回答,由SO网友:Richard Neumann 整理而成

我用以下方法绕过了这个问题images.php.

<?php
// Import wordpress API.
require_once(\'../../../wp-load.php\');


function hinews_get_image($id) {
    if (array_key_exists(\'mimetype\', $_GET)) {
        $mimetype = $_GET[\'mimetype\'];
    } else {
        return \'No MIME type specified.\';
    }

    $options = get_option(\'homeinfo_news_options\');
    $parm_token = \'?access_token=\' . $options[\'token\'];
    $base_url = \'https://myurl.com/\';
    $image_url = $base_url . $id . $parm_token;
    $image = file_get_contents($image_url);

    if ($image === FALSE) {
        return \'Could not retrieve image.\';
    }

    header(\'Content-type: \' . $mimetype);
    return $image;
}


if (array_key_exists(\'id\', $_GET)) {
    echo hinews_get_image($_GET[\'id\']);
}

echo \'No image ID specified.\'
?>
我不知道wordpress API是否提供了一个更干净的解决方案。我当然找不到。

结束

相关推荐

WordPress URLs without posts

我们正在开发基于WordPress的更大系统,但WordPress只用于“静态”内容,系统的主要部分应该是使用外部API和显示数据。我的观点是:我是否能够告诉URL重写不要对某些URL使用WordPress内部系统,而使用其他系统?E、 g。mysite.com/news/news-title 会显示帖子,但是mysite.com/customcontent/anotherlink 将调用某种机制从API加载数据并显示它。我不知道WordPress是否能做到这一点。。谢谢你的观点。