The big thing is that you need to store the HTML in a variable and then return it at the end of the function. As a very basic example…
add_filter ('the_content', 'my_content_filter');
function my_content_filter ($content) {
$extra_html = '<p>Here is my extra HTML</p>';
return $content.$extra_html;
}
You can also set up object caching to do the same thing if you want to use outputs…
add_filter ('the_content', 'my_content_filter');
function my_content_filter ($content){
ob_start ();
?>
<p>Extra content here.</p>
<?php
$extra_html = ob_get_contents ();
ob_end_clean ();
return $content.$extra_html;
}
But being honest, if it was me I’d set up a child theme and modify the templates that are used to display posts, pages, etc, to add in that after the content is displayed. You can still call a central function for that, but you won’t need to worry about doing anything else.