0

My website is a daily deals and offers site. I promote many online stores with affiliate links.

I have created a php script to detect any merchant's link (ex- Amazon) and convert it to my affiliate link.

Example - (Script Name: redirect.php)

If you go to - https://example.com/redirect.php?link=https%3A%2F%2Fwww.amazon.com%2F

It will land you to Amazon site with my affiliate id attached to the url.

My Requirement:-

I have a separate custom field called "rehub_offer_product_url" where I put the normal link to merchant (1 field, 1 link per post).

Now I want the codes which can automatically encode the url of this field and add this part - "https://example.com/redirect.php?link=" before the encoded url when the post is saved (or drafted or published) so that I can fully automate the process of making a normal link to my affiliate link.

It's ok if I see the transformed url next time when I go to edit the post, it just needs to work for the first time when I create and save or publish the post so that I don't need to do it manually.

P.S. Don't forget to mention where I need to add the code!

Please help me to make this happen and you'll be BLESSED. :) :)

  • I'm not quite sure what would be the entire URL with your affiliated link appended to it, whether it's encoded like: https://example.com?redirect=https://amazon.com/product-name&affl=akash34234241, or the affiliation code will be visible on the URL like: https://example.com?redirect=https://amazon.com/product-name&affl=akashaffiliatedlink. Please note on the first case the affiliation link is encoded, and it'd be complex, but on the second case the affiliation link is barely visible and it can be done easily at once. – Mayeenul Islam Jan 15 at 5:28
  • No sir, I have already taken care of that. I just need the same link to be encoded and sent through GET method with "example.com/redirect.php?link=". That's it. – Akash Jan 15 at 5:38
0

I'm not in your scenario, please find out the way you can use the code to get the appropriate flavor.

The issue is when you are appending the actual URL to the current URL, that would be a mess. Let's do it differently:

Let's make a hashed affiliation link and store it in database to verify next time.

<?php
function wpse325608_create_affiliate_link($url, $code) {
    $link = $url .'?affiliate='. $code;
    $affiliate_hashed_link = hash_hmac('md5', $link, 'unique_key');
    return array(
        'url' => $link,
        'hashed' => $affiliate_hashed_link,
    );
}

/**
 * Create Affiliate Redirection Link.
 *
 * This function will append hashed affiliate link to the site URL.
 * example: https://example.com/?redirect=308a316686b55314f21429ba75d84dd3
 *
 * @see wpse325608_create_affiliate_link() for generating your encoded affiliate link.
 * ...
 */
function wpse325608_create_affiliate_redirection_link($url, $code) {
    $affiliate_link = wpse325608_create_affiliate_link($url, $code);
    $redirect_link  = add_query_arg('redirect', $affiliate_link['hashed'], site_url());
    return $redirect_link;
}

Create each affiliate link and store it in options table as a transient, if you want a temporary affiliate link. With transients you can set maximum validity time for each of the link.

If you want a non-perishable link, you can store simply with add_option().

For each of the link, when creating, save it like. Figure out the way you can do it:

<?php
$affiliation_link = wpse325608_create_affiliate_redirection_link('https://amazon.com', 'my_code');
// store for further use.
add_option($affiliation_link['hashed'], $affiliation_link['url']);

Put the following code into where you want to place the affiliate link, from where you want to link to the external link:

<a href="<?php echo esc_url( wpse325608_create_affiliate_redirection_link('https://amazon.com', 'my_code') ); ?>" class="btn-affiliate" target="_blank" rel="noopener">
    Click to Affiliate
</a>

It will generate a link like below:

<a href="https://example.com/?redirect=308a316686b55314f21429ba75d84dd3" class="btn-affiliate" target="_blank" rel="noopener">
    Click to Affiliate
</a>

Now, put the following code in functions.php (or in your plugin file):

<?php
/**
 * Make Redirection if the URL consists 'redirect'.
 * ...
 */
function wpse325608_redirect() {
    $_redirect = filter_input( INPUT_GET, 'redirect', FILTER_SANITIZE_URL );

    if( ! $_redirect ) return;

    $actual_link = get_option($_redirect);

    wp_redirect( esc_url($actual_link) );
    exit();
}
add_action('template_redirect', 'wpse325608_redirect');

The whole idea was shared from our plugin "Download via Email" (available on Github), might not completely fit with your scope of work. But you can think differently with this.

Further Reading

  1. hash_hmac() - PHP Function
  2. set_transient() - WordPress Function
  3. get_transient() - WordPress Function
  4. add_option() - WordPress Function
  5. get_option() - WordPress Function
  6. Download via Email - WordPress plugin by nanodesigns
  • Thanks for your answer, but it's not something I'm looking for. I just want to modify the value of the custom field before the post is published. Like if I put the Amazon's homepage link in the field and publish the post, it should be transformed to this - example.com/redirect.php?link=https%3A%2F%2Fwww.amazon.com%2F – Akash Jan 15 at 13:46
  • add_query_arg() is your answer then. – Mayeenul Islam Jan 15 at 13:50
  • You are not actually getting my point. If possible, please check your facebook page's inbox, I have messaged you there, I believe you can solve this in few minutes, but not getting my point. Thanks in advance! – Akash Jan 16 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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