为了获得PHP和Wordpress技能,我每天都在尝试编写一个小插件。今天的项目是创建一个插件,搜索我帖子中的每个单词,检查是否http://reddit.com/r/$word是有效链接,如果是,则将链接添加到word。
插件在第一篇文章中正常工作。
"**trees**" links to **http://reddit.com/r/trees**
"**php**" links to **http://reddit.com/r/php**
然而,对于每一篇其他帖子,$word变量不会改变。
"**trees**" links to **http://reddit.com/r/$word**
"**php**" links to **http://reddit.com/r/$word**
在过去的一个小时里,我一直在挠头,试图让这个插件正常工作。代码如下:
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, " ");
}
猜猜这个问题是什么?