admin管理员组文章数量:1122846
'm having difficulties figuring out what filters or actions should I write when I want to keep any params ('?ref={username}') in my URLs when user is navigating through the site AND is redirected to another domain.
How do I always keep '?ref={username}' param with every action link user clicks.
I cannot use add_query_var() , because if this is added, then user is redirected to /blog page, which is set up in my link permastructure for blogs/single posts/post templates/search/categories/pagination pages.
I found the following code can be used to extract the values and include, but I don't know what hooks to use.
$referralValue = isset($_GET['ref']) ? $_GET['ref'] : $_GET['r']; add_query_arg( 'ref', $referralValue, $custom_redirect_url ); Any suggestions, pointers would be appreciated, thanks.
What I tried:
Using bricks builder, so I tried a filter for add_filter( 'bricks/auth/custom_redirect_url',....) . But it states in documentation that it's tied with auth flows.
Also wanted to add add_query_var(), but this one breaks my site, because user is redirected to /blog page, which is set up in my link permastructure for blogs/single posts/templates/search/categories/pagination pages.
'm having difficulties figuring out what filters or actions should I write when I want to keep any params ('?ref={username}') in my URLs when user is navigating through the site AND is redirected to another domain.
How do I always keep '?ref={username}' param with every action link user clicks.
I cannot use add_query_var() , because if this is added, then user is redirected to /blog page, which is set up in my link permastructure for blogs/single posts/post templates/search/categories/pagination pages.
I found the following code can be used to extract the values and include, but I don't know what hooks to use.
$referralValue = isset($_GET['ref']) ? $_GET['ref'] : $_GET['r']; add_query_arg( 'ref', $referralValue, $custom_redirect_url ); Any suggestions, pointers would be appreciated, thanks.
What I tried:
Using bricks builder, so I tried a filter for add_filter( 'bricks/auth/custom_redirect_url',....) . But it states in documentation that it's tied with auth flows.
Also wanted to add add_query_var(), but this one breaks my site, because user is redirected to /blog page, which is set up in my link permastructure for blogs/single posts/templates/search/categories/pagination pages.
Share Improve this question asked Apr 3, 2024 at 13:13 ArdiArdi 112 bronze badges2 Answers
Reset to default 1It seems there’s a 2 part solution for this.
First, to detect any ref code in the URL and store the ref into cookies. Secondly, use javascript to add any link click event listener to include the ref.
Detect ref from URL and store it in cookies, I added both scripts to the body (footer) :
<script>
function storeRefInCookie() {
const urlParams = new URLSearchParams(window.location.search);
const ref = urlParams ? (urlParams.get('ref') || urlParams.get('r')) : null;
if (ref) {
const maxAge = 60 * 60 * 24 * 2; // 2 days
const cookieValue = ref + '; path=/;' + 'max-age=' + maxAge + ';' + 'SameSite=None; Secure;';
document.cookie = 'ref=' + cookieValue
document.cookie = 'referredByUser=' + cookieValue;
}
}
storeRefInCookie();
</script>
Add event listener
<script>
// Function to get a cookie value
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
// Add event listener to all link buttons
document.querySelectorAll('a').forEach(function(link) {
link.addEventListener('click', function(event) {
event.preventDefault(); // Prevent the default action
var href = this.getAttribute('href');
var ref = getCookie('ref'); // Get 'ref' cookie value
if (ref) {
href += (href.indexOf('?') !== -1 ? '&' : '?') + 'ref=' + ref;
}
window.location.href = href; // Navigate to the new URL
});
});
</script>
Also, if anyone is looking for what filters to use for WP user navigation to write some customizations, here's the list (I didn't need it):
add_filter('the_permalink', 'add_cookie_value_to_permalink');
add_filter('page_link', 'add_cookie_value_to_permalink');
add_filter('post_link', 'add_cookie_value_to_permalink');
add_filter('term_link', 'add_cookie_value_to_permalink');
add_filter('tag_link', 'add_cookie_value_to_permalink');
add_filter('category_link', 'add_cookie_value_to_permalink');
You can use template_redirect
hook and using this you can append the $ref={username} to the next url.
function append_param_redirect(){
$location = esc_url(add_query_arg('ref', $_GET['ref']));
wp_redirect($location);
}
add_action('template_redirect', 'append_param_redirect');
本文标签: multisiteKeepadd URL referral params in wordpress with any action click
版权声明:本文标题:multisite - Keepadd URL referral params in wordpress with any action click 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311548a1934778.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论