Why does my shortcode get executed before other co

2019-01-20 12:56发布

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 output 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" />

标签: php wordpress
2条回答
孤傲高冷的网名
2楼-- · 2019-01-20 13:43

Your shortcode handler is supposed to return the output to display in place of the shortcode, not output anything itself.

http://codex.wordpress.org/Shortcode_API

查看更多
对你真心纯属浪费
3楼-- · 2019-01-20 13:44

You can use as alternative:

ob_start();
...your code...
return ob_get_clean();

and use without return the html as string.

查看更多
登录 后发表回答