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

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="https://wordpress.stackexchange.com/questions/325608/<?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