Create an exit or redirect page

In this article, we explain how to create a redirect or exit page. 

Heads-up!

The code in this example was updated on July 26, 2022 to work with the Comparison Sets plugin. If you are using the Comparison Sets plugin, you should update your code to the code below.

Here's an example:

Example of an "Exit Page"
1

Create a custom plugin

If you haven't done so already, create a custom plugin.

2

Add custom code

Add the following custom code to your custom plugin:

add_filter( 'dfrapi_after_affiliate_id_insertion', function ( $url, $product, $affiliate_id ) {

	// @var Dfrcs $compset
	global $compset;

	return isset( $_GET['dfrapi_id'] )
		? $url
		: add_query_arg(
			[
				'dfrapi_id'  => absint( $product['_id'] ),
				'dfrcs_hash' => $compset->source->hash ?? null,
			],
			trailingslashit( get_home_url() ) . 'dfrapi-redirect'
		);

}, 20, 3 );

add_filter( 'template_include', function ( $template ) {

	$id         = trim( $_GET['dfrapi_id'] ?? '' );
	$dfrcs_hash = trim( $_GET['dfrcs_hash'] ?? '' );

	if ( empty( $id ) ) {
		return $template;
	}

	$id = absint( $id );

	if ( ! empty( $dfrcs_hash ) ) {

		$compset = dfrcs_select( $dfrcs_hash );

		if ( ! $compset ) {
			return $template;
		}

		$products = maybe_unserialize( $compset['products'] );

		if ( empty( $products ) ) {
			return $template;
		}

		$product_id = 'id_' . $id;

		if ( isset( $products[ $product_id ] ) ) {
			$product = $products[ $product_id ];
		} else {
			return $template;
		}

	} else {

		if ( ! $post = dfrps_get_post_by_product_id( $id, 'product' ) ) {
			return $template;
		}

		$product = dfrps_product( $post['ID'] );

		if ( empty( $product ) ) {
			return $template;
		}
	}

	$redirect_url = dfrapi_url( $product );
	$name         = $product['name'] ?? '';
	$merchant     = $product['merchant'] ?? '';
	$merchant_id  = absint( $product['merchant_id'] ?? 0 );
	?>

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <meta name="robots" content="noindex">
        <meta http-equiv="refresh" content="3;url=<?php echo esc_url( $redirect_url ); ?>">
        <title>Redirecting to <?php esc_html_e( $name ); ?></title>
        <style>
            body {
                margin-top: 100px;
                font-family: sans-serif;
                font-size: 16px;
            }

            .wrapper {
                margin-right: auto;
                margin-left: auto;
                text-align: center;
                padding: 30px;
                max-width: 400px;
                border-radius: 25px;
                box-shadow: 0 10px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19) !important;
            }

            img {
                border: 0;
            }
        </style>
    </head>
    <body>
    <div class="wrapper">
        <p>
            You are being redirected to <strong><?php esc_html_e( $merchant ); ?></strong>&hellip;
        </p>
        <p>
			<?php printf( '<img src="https://images.datafeedr.com/m/%d.jpg" alt="%s Logo" />', $merchant_id, esc_attr( $merchant ) ); ?>
        </p>
        <p>
            If you don't wish to wait, <a href="<?php echo esc_url( $redirect_url ); ?>">click here</a>.
        </p>
    </div>
    </body>
    </html>

	<?php
	// Do NOT edit below this line.
	die();
}, 99 );
	

Now your 'buy' links will display a redirect message and 3 seconds later, load the merchant's website.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us