Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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_end_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?

share|improve this question
    
Maybe that's a function that could be added to PHP itself. –  hakre Aug 30 '10 at 14:10
    
There are a few functions that already have that behavior. bloginfo() is paired with get_bloginfo() to generate a return rather than an echo. There are several other functions that serve the same purpose. My counter question would be, what are you trying to do where you'd need such a global function? –  EAMann Aug 30 '10 at 15:31
    
@hakre - While I agree with the potential need for such a function WordPress Answers is not the appropriate venue to discuss - you should discuss on wp-hackers and/or add a ticket to trac. This question by it's nature it likely not definitive and if there were an telling you why it's not there what value would that have? The question is really more part feature request and veiled rant. I vote to close. –  MikeSchinkel Aug 30 '10 at 18:55
    
@MikeSchinkel: Very true, just wanted to checkout the venue and test boundaries. I see from the comments answers that this is in the wrong direction. The value in knowing why it's not in might help to better understand and find a way to get it in. –  hakre Aug 30 '10 at 20:21

3 Answers 3

up vote 2 down vote accepted

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).

share|improve this answer
    
To jump into the discussion about that aspect, I just thought, why reinvent the wheel. Like if it's coming with WP not every plugin author would need to care. I was not aware that there is a guidance to make own functions return and not echo. –  hakre Sep 1 '10 at 13:57

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.

share|improve this answer
    
no, I was not asking that specifically. Imagine a plugin offers a new template tag but it's designed to work in the loop only. But you need to extract the data from within the loop, save it in a variable and use it later outside of the loop. Something like that scenario. –  hakre Aug 30 '10 at 20:22
    
I see. Maybe clarify what you men by 'later on' in your question. Would it be stored in an array? Or would the variable name be dynamic to avoid being overwritten in the loop? –  kevtrout Aug 30 '10 at 20:35

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:

function get_output($function){
    return call_user_func('get_'.$function);
}

Writing get_the_title() is also shorter than writing get_output('the_title') :)

share|improve this answer
1  
I believe the question has directed at functions that have no get_ equivalent, so you would have to use output buffering. –  Joe Hoyle Aug 31 '10 at 16:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.