There are some pages on my BuddyPress install that I would like to hide by printing a 404 error.
With the help of the following question: Redirect Restricted Page to 404 I came up with this code:
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 );
exit();
Most of the page is hidden nicely and a 404 error is printed, however what has been printed before this code remains (including the header title of the page I want hidden).
Unfortunately I can't move this code higher up, before anything gets printed.
So is there a way to clear everything that has been printed before and then print a fresh 404 error but with keeping the current URL? (So no redirection to the 404.php page.)