user1136783
user1136783

Reputation: 41

how to retrieve custom field at root directory for wordpress?

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

Upvotes: 0

Views: 442

Answers (1)

Janis Elsts
Janis Elsts

Reputation: 764

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

Upvotes: 1

Related Questions