I'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.
It 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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With