Reputation: 20444
The following function is part of code written into the core of a plugin I am reverse engineering. The problem with it is that I need to do an str_replace on it and I cannot because it is already set to echo.
The function is.
function similar_posts($args = '') {
echo SimilarPosts::execute($args);
}
I call it in my pages using similar_posts()
, but what I really need to do in my theme is call $related = similar_posts()
, however that function is set to echo. How do I change that.
I tried this.
function get_similar_posts($args = '') {
SimilarPosts::execute($args);
}
But that did not produce any results.
Upvotes: 1
Views: 457
Reputation: 63797
If you want to use the value SimilarPosts::execute ($args)
returns, you'll need to use the keyword 'return' inside your get_similar_posts
.
function get_similar_posts ($args = '') {
return SimilarPosts::execute($args);
}
If you are unable to change the definition of get_similar_posts
there are ways to snatch the content printed by similar_posts
even though it's "set to echo".
This can be accompished by using the Output Control Functions available in PHP.
function echo_hello_world () {
echo "hello world";
}
$printed_data = "";
ob_start ();
{
echo_hello_world ();
$printed_data = ob_get_contents ();
}
ob_end_clean ();
echo "echo_hello_world () printed '$printed_data'\n";
output
echo_hello_world () printed 'hello world'
Upvotes: 2
Reputation: 31730
function get_similar_posts($args = '') {
return (SimilarPosts::execute($args));
}
Upvotes: 2
Reputation: 270607
return
from the function:
function get_similar_posts($args = '') {
return SimilarPosts::execute($args);
}
Upvotes: 1
Reputation:
Use return
instead of echo.
So that you have:
return SimilarPosts::execute($args);
instead of:
echo SimilarPosts::execute($args);
Upvotes: 1
Reputation: 20444
Done it..
function get_similar_posts($args = '') {
return SimilarPosts::execute($args);
}
and on the page get_similar_posts();
Should have thought of that.
Upvotes: 1