0

i have create a redir.php and placed in the home directory of wordpress to do the redirect. I want to pass in the post id and then retrieve a custom field value of the post and feed into header('location:'.$url);

www.mysite.com/redir.php?id=30 in redir.php will retrieve post id=30 custom field value and pass it into $url.

this is what I have, but it's not working. It gives me "Parse error: parse error in \redir.php on line 5".

it looks like wordpress environment is not being loaded.

 <?php

    require('./wp-blog-header.php');
      $id = $_GET['id'];
  $url= get_field('deal_http_link',post->$id);
    header('Location:'.$url);
?>

thanks

user1136783
  • 41
  • 1
  • 4

1 Answers1

1

Your script has multiple issues:

  • There is whitespace before the opening <?php tag, so the redirect wouldn't work because the headers will have already been sent. Instead, <?php should be the very first thing in the file.
  • post->$id is invalid syntax. You probably meant the $id variable which you defined in the preceding line.
  • To retrieve the value of a custom field, use get_post_meta(), not get_field().

Try something like this instead:

<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url = get_post_meta($id, 'deal_http_link', true);
header('Location: ' . $url);
exit();
Janis Elsts
  • 754
  • 3
  • 11
  • thanks. I was able passing in the id correctly by echo $id. However, it returns a blank page. I tried to echo out the $url but nothing came out. I checked the database, wp_postmeta and the meta_key is "deal_http_link" and post_id i used to check is "935". thanks – user1136783 Aug 19 '12 at 16:46
  • Somehow make me think that wordpress is not being fully loaded, therefore, when use "get_post_meta", it doesn't return anything. this "redir.php" is being called at the home directory. Like this "http://localhost/redir.php?id=935" Thanks – user1136783 Aug 19 '12 at 16:52
  • What *does* get_post_meta() return? False/NULL/an empty string? Also, are you sure the post in question actually has the required custom field. – Janis Elsts Aug 19 '12 at 17:22
  • get_post_meta() returned NULL. I went into mysql table and looked the ost_meta and confirmed that 'deal_http_link" exists and it has a url value next to the post_id. – user1136783 Aug 20 '12 at 05:23
  • got it working! Thanks very much :) how do I put a delay before the jump to display some message with html code? thanks – user1136783 Aug 20 '12 at 05:44
  • got it, just use 'refresh' instead of location :) – user1136783 Aug 20 '12 at 05:53
  • You could try a [JavaScript redirect](http://stackoverflow.com/a/4745622/529673) with setTimeout. – Janis Elsts Aug 23 '12 at 05:40