Including non-wordpress pages within a single multisite domain

You could check for the referring page by using the wp_get_referer() function, which returns the referring page . See https://codex.wordpress.org/Function_Reference/wp_get_referer .

But note that the function uses the $_SERVER['HTTP_REFERER'] value, which is set by the client, and can be spoofed (or disabled) by the client.

So another alternative is to set a global variable that contains the last page ID visited. Then check if your page with your custom function has the proper ‘last-page-id’ value set, which tells you that the custom-php page is being called from an authorized location.

A shortcode could be created (as mentioned in the comment, but that doesn’t really require a custom plugin, you could just include the shortcode stuff in your functions.php of your child theme) that would return the HTML to display the custom function.

You could also create your custom function to include logic that checks for the correct site_id value; if that value is not correct, you can bypass your custom function code.