/** * Add persona, pillar, and traffic_type data to the head section and include them in the dataLayer script */ function add_datalayer_with_additional_fields() { if (is_admin()) return; $persona = ''; $pillar = ''; $traffic_type = ''; // Check for ACF fields on both pages and posts if (function_exists('get_field') && (is_page() || is_single())) { $persona_field = get_field('personas'); // ACF field for personas $pillar_field = get_field('pillar'); // ACF field for pillar $traffic_type_field = get_field('traffic_type'); // ACF field for traffic type // Convert ACF field data to strings $persona = is_array($persona_field) ? implode(', ', $persona_field) : $persona_field; $pillar = is_array($pillar_field) ? implode(', ', $pillar_field) : $pillar_field; $traffic_type = is_array($traffic_type_field) ? implode(', ', $traffic_type_field) : $traffic_type_field; } // Get metadata for the page or post $page_name = get_the_title(); $post_type = get_post_type(); $author = get_the_author_meta('display_name', get_post_field('post_author', get_the_ID())); $published_date = get_the_date(); $categories = get_the_category() ? wp_list_pluck(get_the_category(), 'name') : []; $tags = get_the_tags() ? wp_list_pluck(get_the_tags(), 'name') : []; // Output the dataLayer script echo ''; } add_action('wp_head', 'add_datalayer_with_additional_fields');