?acd系列 – genesis_before
Hook
<?php
function add_query_params_to_links() {
?>
<script>
document.addEventListener("DOMContentLoaded", function() {
const urlParams = new URLSearchParams(window.location.search);
// 從當前 URL 獲取查詢參數
const acdParam = urlParams.get('acd');
const utmSource = urlParams.get('utm_source');
const utmMedium = urlParams.get('utm_medium');
const utmCampaign = urlParams.get('utm_campaign');
// 定義一個函數來附加參數到連結
function appendParamsToLinks(selector, params) {
const links = document.querySelectorAll(selector);
links.forEach(link => {
let href = link.getAttribute('href');
if (params) {
href += (href.includes('?') ? '&' : '?') + params;
link.setAttribute('href', href);
}
});
}
// 如果存在 'acd' 參數,則附加到連結
if (acdParam) {
const acdParamString = 'acd=' + acdParam;
appendParamsToLinks('a.button', acdParamString);
appendParamsToLinks('a.tribe-events-calendar-list__event-featured-image-link', acdParamString);
}
// 如果存在 UTM 參數,則附加到連結
if (utmSource && utmMedium && utmCampaign) {
const utmParamsString = `utm_source=${utmSource}&utm_medium=${utmMedium}&utm_campaign=${utmCampaign}`;
appendParamsToLinks('a.button', utmParamsString);
appendParamsToLinks('a.tribe-events-calendar-list__event-featured-image-link', utmParamsString);
}
});
</script>
<?php
}
add_action('wp_footer', 'add_query_params_to_links');
?>
最新版本
<?php
function add_query_params_to_links() {
?>
<script>
document.addEventListener("DOMContentLoaded", function() {
const urlParams = new URLSearchParams(window.location.search);
// 從當前 URL 獲取查詢參數
const acdParam = urlParams.get('acd');
const utmSource = urlParams.get('utm_source');
const utmMedium = urlParams.get('utm_medium');
const utmCampaign = urlParams.get('utm_campaign');
// 定義一個函數來附加參數到連結
function appendParamsToLinks(selector, params) {
const links = document.querySelectorAll(selector);
links.forEach(link => {
let href = link.getAttribute('href');
if (params) {
href += (href.includes('?') ? '&' : '?') + params;
link.setAttribute('href', href);
}
});
}
// 如果存在 'acd' 參數,則附加到連結
if (acdParam) {
const acdParamString = 'acd=' + acdParam;
appendParamsToLinks('a.button', acdParamString);
appendParamsToLinks('a.tribe-events-calendar-list__event-featured-image-link', acdParamString);
appendParamsToLinks('a.tribe-events-calendar-list__event-title-link', acdParamString);
}
// 如果存在 UTM 參數,則附加到連結
if (utmSource && utmMedium && utmCampaign) {
const utmParamsString = `utm_source=${utmSource}&utm_medium=${utmMedium}&utm_campaign=${utmCampaign}`;
appendParamsToLinks('a.button', utmParamsString);
appendParamsToLinks('a.tribe-events-calendar-list__event-featured-image-link', utmParamsString);
appendParamsToLinks('a.tribe-events-calendar-list__event-title-link', utmParamsString);
}
});
</script>
<?php
}
add_action('wp_footer', 'add_query_params_to_links');
?>