We have template tags and some functions that start with get. Sometimes it would be just nice in themes to do like:
$title = the_title();
to use the html later on. This is just a simplified example, naturally there is some function like get_the_title(); But that works for that function only.
I’m wondering why there is no such function like this:
function get_output() {
$args = func_get_args();
$callback = array_shift($args);
ob_start();
call_user_func_array($callback, $args);
return ob_get_clean();
}
That could convert any function that has output into a returning function:
$title = get_output('the_title');
Any idea why about that has never been thought about? Every theme author or hacker can make use of such, right?
In direct response to the question, WordPress does not include a function for this partly because it does not specifically apply to WordPress functionality. I.e. it’s a PHP (potential) problem, not WordPress.
Also, I wouldn’t say it’s WordPress’ responsibility to provide workarounds for plugins etc that don’t provide an function to return data (which is against the general WordPress style).
I would argue that
get_posts
is the WP answer for your request. Template tags have been created for use in The Loop, but it’s easy enough for any WP ‘hacker’ to use get_posts() instead, and assign, for example: $post->post_title to a variable. It is frustrating at first, to stop relying on The Loop, but easy enough to move past.As it might be easier for some theme developers, you have to take it the way it is done. But you can write the function yourself:
Writing get_the_title() is also shorter than writing get_output(‘the_title’) 🙂