Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

To get my PHP and Wordpress skills, I've been trying to code a small plugin a day. Today's project was to create a plugin that searched over every word in my posts, checked if http://reddit.com/r/$word was a valid link, and added the link to the word if it was.

The plugin works correctly for the first post.

"**trees**" links to **http://reddit.com/r/trees**

"**php**" links to **http://reddit.com/r/php**

However, for every other post, the $word variable does not change.

"**trees**" links to **http://reddit.com/r/$word**

"**php**" links to **http://reddit.com/r/$word**

I've been scratching my head for the past hour trying to get this plugin to work. Here's the code:

add_filter('the_content', 'subreddit_replacer');

function subreddit_replacer($content) {


  global $id;
  $post_id = get_post($id);
  $post_content = $post_id -> post_content;
  //delete_post_meta($id, 'jordan'); die;
  $derp = get_post_meta($id, 'jordan');

  if (empty($derp)) {
  $words = preg_split('@[\W]+@',$post_content);
  $derp = array();
  foreach ($words as $word) {

    $link = "http://reddit.com/r/$word";
    $x = get_headers($link);
    $wordf = ($x[6] == 'HTTP/1.0 200 OK') ? '<a href= "' . $link . '" >' . $word . '</a>' : $word;
    $derp[] = $wordf;

  }
    add_post_meta($id, 'jordan', $derp, 1);
  }

  $derp = is_array($derp[0]) ? $derp[0]: $derp;

  return implode($derp, " ");

}

Any guesses on what this problem could be?

share|improve this question

1 Answer 1

Stop! Stop! You will make your site supper slow / can be banned on reddit (if that ever be posible).

  1. You can try to use save_post and filter your post_post content body.
  2. You can setup it a crop scheldules to parse your post's.

as for your code... Read about get_post_meta last optional attribute there is difference in return if you will use that last attribute. Thats can be a problem isn't it?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.