Possible Duplicate:
problem with short code
Shortcodes are broken.
For those of you playing at home, heres an easy step by step to replicate my problem.
1) Open a fresh WordPress install (3.4.2).
2) Go into twentyeleven/functions.php and add the following:
function test() {
echo '-TEST-';
}
add_shortcode('testshortcode', 'test');
3) Edit the ‘hello world’ post to say:
Welcome to WordPress. This is your first post. Edit or delete it, then start blogging! [testshortcode] foobar
4) Save and view, my results look like the following:
-TEST- Welcome to WordPress. This is your first post. Edit or delete it, then start blogging! foobar
when it should look like this:
Welcome to WordPress. This is your first post. Edit or delete it, then start blogging! -TEST- foobar
What on earth is going on here?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Shortcode callbacks have to return, not output. So use the following:
function test() {
return '-TEST-';
}
add_shortcode( 'testshortcode', 'test' );
More info: http://codex.wordpress.org/Shortcode_API
If you have to use echo you can also do it this way(useful if there’s a lot of markup & it’s difficult working with strings)-
function test() {
ob_start();
echo '-TEST-';
return ob_get_contents();
}
add_shortcode( 'testshortcode', 'test' );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0