A cleaner way than my first suggestion would be this:
- Run the shortcode handler two times: 1. before the content is parsed, 2. during the regular content parsing.
- Store the data you want to prepend in a variable.
It is important to use 'the_post'
as first entry. Otherwise you can get strange side effect in cases where a widget or some other code uses the_content
filters.
add_shortcode( 'test', array ( 'WPSE_77804_Shortcode', 'shortcode_handler' ) );
add_action( 'the_post', array ( 'WPSE_77804_Shortcode', 'store_output' ) );
class WPSE_77804_Shortcode
{
protected static $storage="";
public static function store_output( $post )
{
// maybe someone else has done this already, so we don't have to try again.
'' === self::$storage && do_shortcode( $post->post_content );
'' !== self::$storage
&& add_filter( 'the_content', array ( __CLASS__, 'prepend_content' ) );
}
public static function shortcode_handler( $attrs, $content="" )
{
if ( '' === self::$storage )
{
if ( isset ( $attrs['title'] ) )
self::$storage .= $attrs['title'];
if ( isset ( $attrs['date'] ) )
self::$storage .= ' ' . $attrs['date'];
}
// you might do much more here and return the complete string
return '|' . self::$storage . '|' . $content;
}
public static function prepend_content( $content )
{
return self::$storage . '<hr>' . $content;
}
}
Related answer for galleries.