logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Redirect exit page: Get external url from custom field

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.

This question has been answered.

Jens Filipsson | 09/26/12 at 7:37am Edit


(17) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    09/26/12
    7:45am
    Francisco Javier Carazo Gil says:

    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
    }


    ?>

  • avatar
    Last edited:
    09/26/12
    7:56am
    Arnav Joy says:

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

  • avatar
    Last edited:
    09/26/12
    7:58am
    John Cotton says:

    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}

  • avatar
    Last edited:
    09/26/12
    8:03am
    Ivaylo Draganov says:

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

  • avatar
    Last edited:
    09/26/12
    7:47am
    Jens Filipsson says:

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

  • avatar
    Last edited:
    09/26/12
    7:48am
    Francisco Javier Carazo Gil says:

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

  • avatar
    Last edited:
    09/26/12
    8:00am
    Arnav Joy says:


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

    ?>

  • avatar
    Last edited:
    09/26/12
    8:01am
    Arnav Joy says:

    see the

    content="2;

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

    content="10;

  • avatar
    Last edited:
    09/26/12
    8:19am
    Jens Filipsson says:

    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?

  • avatar
    Last edited:
    09/26/12
    8:31am
    Arnav Joy says:

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

    ?>

  • avatar
    Last edited:
    09/29/12
    5:16am
    Jens Filipsson says:

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

  • avatar
    Last edited:
    09/29/12
    5:23am
    Ivaylo Draganov says:

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

  • avatar
    Last edited:
    09/29/12
    5:26am
    Jens Filipsson says:

    Blank screen

  • avatar
    Last edited:
    09/29/12
    5:46am
    Ivaylo Draganov says:

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

    I've just tested the code and it works.

  • avatar
    Last edited:
    09/29/12
    5:59am
    Jens Filipsson says:

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

    ?>


  • avatar
    Last edited:
    09/29/12
    6:03am
    Ivaylo Draganov says:

    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.

  • avatar
    Last edited:
    09/30/12
    4:49pm
    Jens Filipsson says:

    It seems to work now, but I had to change esc_url to esc_url_raw. 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.

This question has expired.



Ivaylo Draganov had additional discourse to offer.

Jens Filipsson, Arnav Joy voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.