Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Redirect exit page: Get external url from custom field WordPress

  • SOLVED

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".

<strong>The problem:</strong>

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.

<strong>So:</strong>

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.

Answers (4)

2012-09-26

Ivaylo Draganov answers:

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 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(); ?>


Jens Filipsson comments:

Unfortunately the functions.php code breaks my page...


Ivaylo Draganov comments:

What happens to the page? Do you get a blank screen or any error messages?


Jens Filipsson comments:

Blank screen


Ivaylo Draganov comments:

Please make sure that there aren't any extra <em><?php</em> or <em>?></em> tags. After pasting the code into functions.php you should remove these extra tags. There has to be only one <em><?php</em> at the beginning of the file.

I've just tested the code and it works.


Jens Filipsson comments:

I know how the functions.php works. Could it be that this chunk of code shouldn't be in functions.php?

<?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' );

?>



Ivaylo Draganov comments:

No, that's where it's supposed to be.

Actually such functionality is better to be wrapped-up as plugin instead of stuffed into functions.php. But as I said the code works.
Can you enable WP_DEBUG to see what the exact error message is - it might be a mere syntax error.

To enable WP_DEBUG add this to wp-config.php:

define('WP_DEBUG', true);


Then revisit your site and see whether an error message is shown on the blank screen.


Jens Filipsson comments:

It seems to work now, but I had to change <strong>esc_url</strong> to <strong>esc_url_raw</strong>. Is it ok to use esc_url_raw instead?

Another thing, this one needs javascript to work right? Is there another way to make the redirect not javascript dependent?

Thanks!
// Jens.

2012-09-26

Francisco Javier Carazo Gil answers:

You will need something like this:

exit.php

<?php
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
$id = $_GET["id"];

$custom_fields = get_post_custom($id);
$my_custom_field = $custom_fields['my_custom_field'];

foreach ( $my_custom_field as $key => $value )
{
echo "You are going to be redirected to: " . $value;
// sleep 5 seconds
// redirect to $value
}


?>


Jens Filipsson comments:

Thanks!
And how do I link to the page and pass the post id paramenter?


Francisco Javier Carazo Gil comments:

If you do directly this: http://mywebsite.com/exit.php?=426 you have to create in your loop a href that contains this link and that recover the post id with get_the_ID().

2012-09-26

Arnav Joy answers:

in the link pass the ?postID as the paramete , like below url

http://mywebsite.com/exit.php?postID=426

<?php
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');

$postID = $_GET["postID"];
$external_url_custom_field = 'external_url' // change here for the url

$external_url = get_post_custom($postID,$external_url_custom_field,true);

echo "Redirecting to: " . $value.' or you can <a href="'.$external_url.'">click here</a>';

?>

<meta http-equiv='refresh' content='2;url=<?php echo $external_url;?>>


Arnav Joy comments:


if you are integrating it to page or post then use following

<?php
$url = get_bloginfo('url').'/exit.php?postID='.get_the_ID();
echo '<a href="'.$url.'" > click here </a> for external url';

?>


Arnav Joy comments:

see the

content="2;

here 2 indicates no. of second
change the 2 to whatever values you want say 10

content="10;


Jens Filipsson comments:

Thanks!
If I would like to get two different values from two different custom fields, and maybe use something like this:

<a href="http://custom-field-url">Custom-field-second-value</a>

Is this possible?


Arnav Joy comments:

use this
<?php

$external_url_custom_field_url = 'external_url' // change here for the url

$external_url_custom_field_title = 'external_url_title' // change here for the title

$external_url = get_post_custom($postID,$external_url_custom_field_url,true);

$external_title = get_post_custom($postID,$external_url_custom_field_title,true);

echo "Redirecting to: " . $external_title.' or you can <a href="'.$external_url.'">click here</a>';

?>

2012-09-26

John Cotton answers:

1. Create yourself a page template (your exit template).

2. Don't user get_header(), just embed the header code you want and add this code after the head tag:


<?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}