Horje
Redirect exit page: Get external url from custom field - Wordpress Solution
Hello! I'm looking for a solution for the following scenario: When I link to external pages, I want to link to an exit page, which displays a goodbye message for a few seconds, and then automatically transfers the visitor to the external url. You've probably seen it: "You are now being redirected to blablabla, if nothing happens you can click the link below". The problem: I don't want to link directly to the external url. Instead I want to link to the exit page, and pass the post id as a paramenter. Something like this: http://mywebsite.com/exit.php?=426 Where 426 is the post ID. THEN, I would like to fetch the external url from a custom field in the original post. So: I need some kind of page template and when I link to this page i can pass the post id as a paramenter or something. Then the page template uses the post id and retrieves the value of a custom field from the post. I want it to redirect after a few seconds, just have enough time so the visitor can read my Goodbye message! Thank you! // Jens.

Solution - 1

Hello, what you're trying to do is fairly simple. It can be accomplished without a page template. We'll have to intrudoce a new query variable, for example 'exit'. So then you'd link to http://example.com/?exit={post_id} Let's go through setting that up. First we register the new query var:


<?php
// this goes into functions.php
function custom_query_vars( $vars ) {
	
	/** Add new query vars */
	array_push( $vars, 'exit' );
	
	return $vars;
}
	
add_filter( 'query_vars', 'custom_query_vars' );
?>
Then, we tell WP to load a special template:

<?php
// this goes into functions.php
function custom_template_redirect() {

	$exit_post_id = get_query_var( 'exit' ) ? get_query_var( 'exit' ) : 0;

	if ( $exit_post_id ) {
		locate_template( 'exit.php', true );
		exit;
	}
}

add_action( 'template_redirect', 'custom_template_redirect' );
?>
Then we create the actual template file in the theme directory:

<?php
// this is the code for exit.php
$exit_post_id = get_query_var( 'exit' ) ? get_query_var( 'exit' ) : 0;
$redirect_url = $exit_post_id ? get_post_meta( $exit_post_id, 'external_url_custom_field', true ) : '';
?>

<?php get_header(); ?>

<?php if ( $redirect_url ) { ?>

	<p>You are now being redirected to blablabla, if nothing happens you can click the link below.</p>
	<p><a rel="nofollow" href="<?php echo esc_url($redirect_url); ?>">The link below</a></p>

	<script type="text/javascript">
		setTimeout("location.href = '<?php echo esc_url($redirect_url); ?>';",1500);
	</script>

<?php } else { ?>

	<p>Invalid redirect URL or post ID.</p>

<?php } ?>
	
<?php get_footer(); ?>


Solution - 2

You will need something like this: exit.php $value ) { echo "You are going to be redirected to: " . $value; // sleep 5 seconds // redirect to $value } ?>


Solution - 3

in the link pass the ?postID as the paramete , like below url http://mywebsite.com/exit.php?postID=426 click here'; ?>

<?php if( isset($_POST['t'] ) { $url = get_post_meta( $_POST['t'], 'my_custom_field_name', true ); ?> <meta http-equiv="refresh" content="5;url=<?php echo $url; ?>"> 3. Create a new page in the dashboard and set the template to your exit template. 4. Set links to the this page as /my-exit-page?t={ID OF POST}





Wordpress

Related
Problem with headway theme css - Wordpress Solution Problem with headway theme css - Wordpress Solution
Related posts within a taxonomy. - Wordpress Solution Related posts within a taxonomy. - Wordpress Solution
Decrease space - Wordpress Solution Decrease space - Wordpress Solution
How to rebuild WordPress permalinks - Wordpress Solution How to rebuild WordPress permalinks - Wordpress Solution
Parent page custom fields - Wordpress Solution Parent page custom fields - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8