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.

I have the following text in a page. As you can see my shortcode is right at the bottom but somehow when the code runs, the out put of my shortcode is inserted at the top of the page instead of following its preceding content.

<img class="alignnone" title="title_enquires" src="http://localhost/barcelona/wp-content/uploads/2011/08/title_enquires.jpg" alt="" width="589" height="77" />
<img class="alignnone" title="contact_map" src="http://localhost/barcelona/wp-content/uploads/2011/08/contact_map.jpg" alt="" width="555" height="222" />

[barcelona_address]

here is my short code registration inside the function.php file:

<?php
add_shortcode( 'barcelona_address', 'barcelona_shortcode_handler' );

function barcelona_address_func()
{
    print "<p>sdsdsds</p>";
}

function barcelona_shortcode_handler( $atts, $content=null, $code="" ) 
{
   if (function_exists($code . "_func"))
   {
       call_user_func($code . "_func", $atts);
   }
}
?>

and the result is:

<p>sdsdsds</p>
<img class="alignnone" title="title_enquires" src="http://localhost/barcelona/wp-content/uploads/2011/08/title_enquires.jpg" alt="" width="589" height="77" />
<img class="alignnone" title="contact_map" src="http://localhost/barcelona/wp-content/uploads/2011/08/contact_map.jpg" alt="" width="555" height="222" />
share|improve this question
3  
Change print to return –  Bainternet Aug 15 '11 at 5:13
    
Thanks that was it –  Am01 Aug 15 '11 at 5:20
1  
@Bainternet why did you not post that as an answer? :) –  Chip Bennett Aug 15 '11 at 11:56
    
@Am01 be sure to mark Bainternet's answer as accepted! –  Chip Bennett Aug 15 '11 at 12:59
    
possible duplicate of The result of a shortcode appear BEFORE page content –  Chip Bennett Apr 11 '12 at 12:08

1 Answer 1

By popular demand:

The solution is simply to replace print to return . Shortcodes should always return the value instead of printing or echoing it out.

share|improve this answer
2  
And the symptom of this mistake is exactly what the OP observed: shortcode content being output before/at the beginning of the_content(). –  Chip Bennett Aug 15 '11 at 12:26

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.