I think, I would simply store all the definitions during the glossary_shortcode()
execution in a global
variable and serve them at the end of some hook like the_content
.
function glossary_shortcode( $atts = array(), $shortcode_content = null ) {
// Your code can be kept identical, just store the $glossary_term_object somewhere.
global $glossaries;
$glossaries = $glossaries ?? [];
$glossaries[] = glossary_term_object;
}
add_filter( 'the_content', function ( $content ) {
global $glossaries;
if ( !empty( $glossaries ) ) {
echo '<ul class="glossary">';
foreach ( $glossaries as $glossary ) {
printf( '<li><a href="%s" target="_blank">%s : %s</a></li>',
get_the_permalink( $glossary ),
$glossary->post_title,
$glossary->post_content
);
}
echo '</ul>';
}
return $content;
}