4

I have a specific requirement that my referral link be as simple as possible to type in manually (customer base is very low skilled) while simultaneously referring to an employee number who was responsible for the referral.

So, the referral links need to look like:

www.domain.com/referral/1555

Where "1555" is the employee ref number who sent this referral link, and it's the employee ref number that I'll be taking out of the URL. I'm using PHP snippets to deal with loading that data and handling it, but I need to land on the "referral" page inorder for that php snippet to fire.

How do I dictate to WP that any URL slug starting with "referral", regarldes of what comes after, goes to my referral page and keeps the URL intact? The best I can do right now is land on the referral page but the URL gets reset to merely

www.domain.com/referral
4
  • Not an answer to your question, but the plugin Pretty Links does this, you may want to look at their code: wordpress.org/plugins/pretty-link
    – Michelle
    Commented Dec 2, 2021 at 17:07
  • 1
    It seems like it's just a link shortner? I don't believe I can grab data out of the URL before WP sanitizes it even if I use that plugin... maybe I'm missing something? Commented Dec 3, 2021 at 3:22
  • Why does processing or pre-processing need to take place after the redirect, instead of prior to it.
    – CK MacLeod
    Commented Dec 3, 2021 at 3:41
  • 1
    I think you need a rewrite rule for this. Check this post wordpress.stackexchange.com/questions/26388/…
    – BlueSuiter
    Commented Dec 3, 2021 at 6:07

2 Answers 2

1

Supposing you have a page created with the slug "referral", you can add another segment to the page URL containing the employee ref number like this:

1- Register a new query variable e.g "employee_ref"

add_filter('query_vars', function ($query_vars){
    $query_vars[] = 'employee_ref';
    return $query_vars;
});

2- Using add_rewrite_rule function, add a new rule for the 'employee_ref' to be equal to the last numeric segment of the page URL:

add_action('init', function (){
    add_rewrite_rule(
        '^referral/([0-9]+)/?$',
        'index.php?pagename=referral&employee_ref=$matches[1]',
        'top'
    );
});

3- Go to permalinks setting page on your WordPress dashboard and hit the "Save Changes" button to flush the rewrite rules--a mandatory step--.

4- The referral link now will look like "https://yourdomain.com/referral/345/", where "345" is the employee ref number and you can catch it in PHP using:

get_query_var('employee_ref');

The code used in the first two steps can be placed inside your theme's functions.php or a plugin file.

1
  • Where does this go? I added this function to my WP's child-theme Functions.php but nothing happened? I even throw an exception within that file and it doesn't change anything... what am I missing? Commented Dec 21, 2021 at 17:05
0

You just need the Redirection Plugin and a little regex. There are good options on how to match the URLs in the plugin settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.