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?
