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.

In the dashboard, I'm using a textarea to get user input. I'd like them to be able to use shortcodes anywhere in that textarea.

Here's my current function:

function slp_getwelcome() {                 
    $slp_welcome = get_option('slp_welcome');   
    $slp_welcome_sc = do_shortcode( $slp_welcome );

    return $slp_welcome_sc;     
}

This partially works.

If I have this text in the textarea:

Lorem ipsum dolor sit amet, consectetur [location] adipiscing elit. Donec sit amet quam id eros fringilla.

The above function outputs this:

Abbott, CALorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sit amet quam id eros fringilla.

Where "Abbott, CA" is the proper result of the [location] shortcode. So the shortcode is called, but it returns at the beginning of the textarea content.

How can I fix this?

Here's the shortcode logic:

function get_location() { 
    global $post;
    $location = strip_tags( get_the_term_list( $wp_query->post->ID, 'locations', '', ', ', '' ) );
    echo $location; 
} 
add_shortcode('location', 'get_location');

A second shortcode reacting the same, despite the use of return:

function get_service() { 
    $servive = single_post_title() ;
    return $service;
} 
add_shortcode('service', 'get_service');
share|improve this question
1  
I think you need to show your shortcode logic. –  vancoder Mar 7 '13 at 20:04
    
@vancoder shortcode function has been added to the original post –  NW Tech Mar 7 '13 at 20:09
    
Any particular reason this post was down voted? --So I can try to avoid that mistake in the future. –  NW Tech Mar 7 '13 at 20:37
    
Haven't down-voted it, but probably it's because this is a common mistake easily found searching this site archives. That's the only flaw I can see, all in all, the Q is well explained, documented and formated, keep'em coming ;) –  brasofilo Mar 7 '13 at 22:02
    
Thanks @brasofilo. I'll keep that in mind in my future questions and responses. –  NW Tech Mar 7 '13 at 22:21
add comment

2 Answers

up vote 1 down vote accepted

You should be returning $location from get_location, instead of echoing it. Your function currently echos the location as soon as it runs, thereby outputting the result before the rest of the copy.

share|improve this answer
    
Perfect! Thank you. I'm still learning all the ins and outs of PHP, but this makes sense now. –  NW Tech Mar 7 '13 at 20:15
    
I have two different shortcodes that might be called in this textarea. I changed both to return instead of echo, however my second shortcode reacts the same as what was described in the original post. The original post has been updated to show the second shortcode. –  NW Tech Mar 7 '13 at 20:23
1  
start reading the corresponding Codex chapter when you are using a WordPress function - codex.wordpress.org/Function_Reference/single_post_title –  Michael Mar 7 '13 at 20:29
    
@Michael Thank you. I used that codex to get that function intially, but I read it further and realized using single_post_title('', false) actually output the title to be used properly for the shortcode. Thanks again. –  NW Tech Mar 7 '13 at 20:40
add comment

Try to return your value, instead of echoing it.

function hello() {
    return '{foobar}';
}

add_shortcode('hw', 'hello');
share|improve this answer
add comment

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.