add_action('wp_head', 'insert_custom_ga4_tag'); function insert_custom_ga4_tag() { // Only run on single posts if (is_single()) { global $post; // Retrieve the custom field value $ga4_id = get_field('ga4_measurement_id', $post->ID); // If an ID exists for this post, output the GA4 tracking script if (!empty($ga4_id)) { ?>