0

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');
5
  • 1
    I think you need to show your shortcode logic.
    – vancoder
    Mar 7, 2013 at 20:04
  • @vancoder shortcode function has been added to the original post
    – NW Tech
    Mar 7, 2013 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, 2013 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, 2013 at 22:02
  • Thanks @brasofilo. I'll keep that in mind in my future questions and responses.
    – NW Tech
    Mar 7, 2013 at 22:21

2 Answers 2

1

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.

4
  • Perfect! Thank you. I'm still learning all the ins and outs of PHP, but this makes sense now.
    – NW Tech
    Mar 7, 2013 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, 2013 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, 2013 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, 2013 at 20:40
1

Try to return your value, instead of echoing it.

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

add_shortcode('hw', 'hello');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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