I have a function whos purpose is to return some HTML ($offer_html) to display on my page. However, I´m seeing some strange things.
Below you see a div is added first to $offer_html (DIV is closing at the end).
$offer_html = '<div class="box middle offer alignleft">';
$offer_html .= '<p class="detail alignleft">' . $volum . '</p>';
$offer_html .= '<p class="detail alignleft">' . $produsent . '</p>';
$offer_html .= wpfp_link();
$offer_html .= '</div';
return $offer_html;
My problem is with the function wpfp_link(). The function returns HTML, but this HTML ends up totally misplaced. When the page renders, the HTML looks like this:
<img class="wpfp-img" title="Favorite" alt="Favorite" src="http://localhost:8888/wordpress/wp-content/plugins/wp-favorite-posts/img/remove.png">
<div class="box middle offer alignleft">
</div>
As you see, the HTML returned by the wpfp_link() ends up outside the DIV which I want it to be inside.
Does anyone know why this happens?
You wpfp_link() does not return the HTML string, it directly echo’s the string. You’ve to catch that from the output buffer like the following:
There is another – and I believe better – way to solve this. Looking at the source code of the plugin containing that function (WP Favorite Posts) you can see that the
wpfp_link()
function accepts various arguments, the first of which is a flag that changes its behavior between printing its output and returning it.so
should act as you expected.
I checked the source code but this is probably documented somewhere in the plugin information.
It’s impossible to tell for sure from what little code you show, but maybe you need to directly
echo
the string instead of returning it.The wpfp_link function is probably echoing the HTML instead of returning it. You could use output buffering get the HTML as a string.
Try printing out (using
echo
) the whole string instead.Is this the cause of any problems:
should surely be
I apologize for answering twice, I noticed a bug with the code for chosen answer. Consider the following code:
The variable $wpfp_html has a value of false for each call of your_function because output buffering is turned off after ob_end_flush is called. ob_start() needs to be called each time prior to ob_get_clean() for the output buffer to be active.
I would’ve posted a comment to your answer, Informant but I don’t have a high enough reputation.