There is probably some caching issue, try using ob_* functions to wrap your shortcode (sample: https://tobymackenzie.wordpress.com/2010/02/28/wordpress-shortcodes/ )
Thread Starter
OM2
(@om2)
thanks for the reply
not working for me
i read and followed instructions – no luck
this is my code:
<?php
/*
Plugin Name: My Plugin
Version: 1.0
Description: My Plugin
*/
add_shortcode( 'myplug', 'myplug' );
function myplug () {
ob_start();
?>
<div>Hello World</div>
<?php }
$fncReturn = ob_get_contents();
ob_end_clean();
return $fncReturn;
?>
I’ve over simplified to illustrate my point
The shortcode is put in the middle of text
But ‘Hello World’ still appears at the top
Where am I going wrong?
Thanks
Omar
Thread Starter
OM2
(@om2)
i posted this question a long time ago
i’m still having the same problem!
is there anyway of controlling when the shortcode code executes?
for me the whole idea of using shortcodes is defeated 🙁
or am i missing something?
Hi OM2 you may have by now figured it out but this will be here to help people in the future. (This part isn’t for you I just added it in) Using return is better than echo simply because it allows the function to return the value instead of ehco which executes immediately. Here is a more complex scenario where we use a function and we need to stop it echoing immediately (“echo=0”) before we pass the information to a variable. This example is getting 5 recent posts and then for each value that is returned we are simply parsing it out as a list item into the variable and then we return the variable.
<?php function poster_thumbnails() {
$recent_posts = wp_get_recent_posts('numberposts=5&echo=0');
foreach( $recent_posts as $recent ){
$ret .= '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
return $ret;
}
add_shortcode("poster","poster_thumbnails"); ?>