开发者

How to return a function inside a function (as opposed to echo)

开发者 https://www.devze.com 2023-02-09 07:30 出处:网络
So, in order to get this code working, I need to output the function \"get_flickr_rss\" via a return rather than an echo... I believe the reason this code is not working for me is because the \"get_fl

So, in order to get this code working, I need to output the function "get_flickr_rss" via a return rather than an echo... I believe the reason this code is not working for me is because the "get_flickr_rss" function itself is somehow defaulting to an echo rather than a return. How can I call the function to force it to return rather than echoing?

function generate_flickr_rss($atts, $content = null) {
    // default parameters
    extract(shortcode_atts(array(
        'set' => '72157625809767439',
        'photos' => '20'
    ), $atts));
    // Call FLickrRSS Hook
    return get_flickrRSS(array('set' => $set, 'num_items' => $photos, 'type' => 'set'));
}

I tried the following code to reverse engineer it as a return, but unfortunately, no dice.

function generate_flickr_rss($atts) {
    // default parameters
    extract(shortcode_atts(array(
        'set' => '72157625809767439',
        'photos' => '20'
    ), $atts));
    // Call FLickrRSS Hook
    $flickr_rs开发者_JAVA技巧s_return = get_flickrRSS(array('set' => $set, 'num_items' => $photos, 'type' => 'set'));
    return $flickr_rss_return;
}

add_shortcode('flickr_rss', 'generate_flickr_rss');

add_shortcode('flickr_rss', 'generate_flickr_rss');


With output buffering you can catch the output of the function.

ob_start();
get_flickrRSS(...);
return ob_get_clean();

Of course you should rather change the function to return it's output, but I guess that's impossible for a reason. ;-)


I took a look at the WP plugin flickrRSS and the changes aren't that great.

In the function printGallery, there are a number of echo statements. Replace these with lines that concatenate the echoed string to a $result variable that your define as an empty string at the beginning of the function.

Then, return $result at the end of the printGallery function. Then, modify get_flickrRSS to return or echo this string according to your needs (as defined by another input parameter).

Of course, all of this will get overwritten the next time the plugin is updated, so I suggest pointing the plugin writer to this Stack Overflow page and asking him to modify the plugin to support your needs long term :)


$foo = get_flickrRSS(.....;
return $foo;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号