php 用于跟踪代码的ACF自定义字段中继器(Wordpress)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 用于跟踪代码的ACF自定义字段中继器(Wordpress)相关的知识,希望对你有一定的参考价值。
function theme_prefix_add_tracking_header() {
$tracking_header = get_field('tracking_repeater_header','option');
if (is_array($tracking_header) || is_object($tracking_header)) {
if ($tracking_header) {
echo PHP_EOL . '<!-- Header analytics -->' . PHP_EOL;
foreach ($tracking_header as $tracking_header_item) {
if (array_key_exists('name', $tracking_header_item)) {
echo '<!-- ' . $tracking_header_item['name'] . ' -->';
}
if (array_key_exists('code', $tracking_header_item)) {
echo $tracking_header_item['code'] . PHP_EOL;
}
}
echo PHP_EOL . '<!-- Header analytics end -->' . PHP_EOL;
}
}
}
add_action( 'wp_head', 'theme_prefix_add_tracking_header' );
function theme_prefix_add_tracking_body() {
$tracking_body = get_field('tracking_repeater_body','option');
if (is_array($tracking_body) || is_object($tracking_body)) {
if ($tracking_body) {
echo PHP_EOL . '<!-- Scripts that go right after opening <body> -->' . PHP_EOL;
foreach ($tracking_body as $tracking_body_item) {
if (array_key_exists('name', $tracking_body_item)) {
echo '<!-- ' . $tracking_body_item['name'] . ' -->';
}
if (array_key_exists('code', $tracking_body_item)) {
echo $tracking_body_item['code'] . PHP_EOL;
}
}
echo PHP_EOL . '<!-- Scripts that go right after opening <body> end -->' . PHP_EOL;
}
}
}
add_action( 'theme_prefix_after_opening_body', 'theme_prefix_add_tracking_body' );
function theme_prefix_add_tracking_footer() {
$tracking_footer = get_field('tracking_repeater_footer','option');
if ($tracking_footer) {
echo PHP_EOL . '<!-- Footer analytics -->' . PHP_EOL;
if (is_array($tracking_footer) || is_object($tracking_footer)) {
foreach ($tracking_footer as $tracking_footer_item) {
if (array_key_exists('name', $tracking_footer_item)) {
echo '<!-- ' . $tracking_footer_item['name'] . ' -->';
}
if (array_key_exists('code', $tracking_footer_item)) {
echo $tracking_footer_item['code'] . PHP_EOL;
}
}
}
echo PHP_EOL . '<!-- Footer analytics end -->' . PHP_EOL;
}
}
add_action( 'wp_footer', 'theme_prefix_add_tracking_footer' );
<?php
// can be in functions.php as well
acf_add_local_field_group(array (
'key' => 'group_59b665d4365e1',
'title' => 'Tracking codes',
'fields' => array (
array (
'key' => 'field_59b666088db92',
'label' => __('Tracking code (header)','theme_domain'),
'name' => 'tracking_repeater_header',
'type' => 'repeater',
'instructions' => __('The tracking codes added here will be included on every page, in the <head> tag of the site markup.','theme_domain'),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => 'field_59b6663e8db93',
'min' => 0,
'max' => 0,
'layout' => 'block',
'button_label' => __('Add tracking code','theme_domain'),
'sub_fields' => array (
array (
'key' => 'field_59b6663e8db93',
'label' => __('Name','theme_domain'),
'name' => 'name',
'type' => 'text',
'instructions' => __('A short name for memorability. Doesn\'t affect anything.','theme_domain'),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array (
'key' => 'field_59b6aec28db94',
'label' => __('Code','theme_domain'),
'name' => 'code',
'type' => 'acf_code_field',
'instructions' => __('Tracking code, including <script> tag','theme_domain'),
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'mode' => 'text/html',
'theme' => 'monokai',
),
),
),
array (
'key' => 'field_mi2u2vpjz6m05',
'label' => __('Tracking code (body)','theme_domain'),
'name' => 'tracking_repeater_body',
'type' => 'repeater',
'instructions' => __('The tracking codes added here will be included on every page, after the opening <body> tag of the site markup.','theme_domain'),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => 'field_it11hbarl8bzk',
'min' => 0,
'max' => 0,
'layout' => 'block',
'button_label' => __('Add tracking code','theme_domain'),
'sub_fields' => array (
array (
'key' => 'field_it11hbarl8bzk',
'label' => __('Name','theme_domain'),
'name' => 'name',
'type' => 'text',
'instructions' => __('A short name for memorability. Doesn\'t affect anything.','theme_domain'),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array (
'key' => 'field_us87uvxrs8t6z',
'label' => __('Code','theme_domain'),
'name' => 'code',
'type' => 'acf_code_field',
'instructions' => __('Tracking code, including <script> tag','theme_domain'),
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'mode' => 'text/html',
'theme' => 'monokai',
),
),
),
array (
'key' => 'field_59b6af0d8db95',
'label' => __('Tracking code (footer)','theme_domain'),
'name' => 'tracking_repeater_footer',
'type' => 'repeater',
'instructions' => __('The tracking codes added here will be included on every page before the closing </body> tag.','theme_domain'),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => 'field_59b6af0d8db96',
'min' => 0,
'max' => 0,
'layout' => 'block',
'button_label' => __('Add tracking code','theme_domain'),
'sub_fields' => array (
array (
'key' => 'field_59b6af0d8db96',
'label' => __('Name','theme_domain'),
'name' => 'name',
'type' => 'text',
'instructions' => __('A short name for memorability. Doesn\'t affect anything.','theme_domain'),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array (
'key' => 'field_59b6af0d8db97',
'label' => __('Code','theme_domain'),
'name' => 'code',
'type' => 'acf_code_field',
'instructions' => __('Tracking code, including <script> tag','theme_domain'),
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'maxlength' => '',
'mode' => 'text/html',
'theme' => 'monokai',
),
),
),
),
'location' => array (
array (
array (
'param' => 'options_page',
'operator' => '==',
'value' => 'acf-options-tracking',
),
),
),
'menu_order' => 1,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
));
以上是关于php 用于跟踪代码的ACF自定义字段中继器(Wordpress)的主要内容,如果未能解决你的问题,请参考以下文章
Wordpress ACF:如何通过自定义代码(PHP)将行添加到附加到用户的转发器字段