What are the best practices for merging custom field data with post content in WordPress using PHP?

When merging custom field data with post content in WordPress using PHP, it is important to properly sanitize and escape the custom field data to prevent any security vulnerabilities. One way to achieve this is by using the WordPress function get_post_meta() to retrieve the custom field data and then using the WordPress function wp_kses() to sanitize the data before merging it with the post content.

$post_id = get_the_ID();
$custom_field_data = get_post_meta($post_id, 'custom_field_name', true);
$custom_field_data_sanitized = wp_kses($custom_field_data, array());
$post_content = get_the_content();

$merged_content = $post_content . $custom_field_data_sanitized;

echo $merged_content;