How to add code to custom field to every blog post via php?

You can use the update_post_metadata hook to edit any custom field value right before it’s saved to the database. For example:

function update_my_post_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
    if ( $meta_key === 'MY_META_KEY' ) {
        $meta_value="BEFORE" . $meta_value . 'AFTER';
        update_post_meta( $object_id, $meta_key, $meta_value );
        return true;
    }
}
add_filter( 'update_post_metadata', 'update_my_post_metadata', 10, 5 );

In that example you would replace MY_META_KEY with your meta key, and BEFORE and AFTER with whatever arbitrary HTML or shortcode you wish to wrap the meta value in.

Alternately — looking at the details in your comment — if you’re just trying to make it so that only registered users can see it, you can do that when you’re outputting the meta value, like this:

if ( is_user_logged_in() ) {
    echo get_post_meta( get_the_ID(), 'MY_META_KEY', true );
}