How Can I Concatenate A String With One Of My Custom Field Value Before Saving The Post?

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. :) :)

Topic functions links custom-field customization Wordpress encoding

Category Web


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.