更改ping back功能

时间:2016-08-19 作者:lucian

我想在多作者网站上充分利用ole pingback,但需要做一些修改和考虑。

它们应仅在内部可用,不接受外部pingback。如果我的理解是正确的,在常规选项中禁用它们,而在每个帖子上保持打开状态应该可以做到这一点-这是正确的吗?

我想改变他们的格式。现在参考页面标题变成了作者,这并不酷。我宁愿让真正的博文作者作为作者the_title() / <h1> 作为标题(新字段),片段作为内容保留在原处。我没有找到太多关于这方面的信息,是否有一个文档化的API(非过时的)或一些钩子/过滤器可以实现这一点?

注意:我想应该在发送端发生一些事情,比如将post id传递给以后使用。我对如何做有一个想法,但这需要在可见内容(如url)上添加帖子id,我不愿意这样做

pingback功能是否将在Wordpress中得到长期支持,或者可能会被弃用。如果它要去任何地方,它可能会像“links”那样有一种遗留的启用,还是我的pingbacks最终会陷入困境?

网络提到更好的主意还是死路一条?

1 个回复
最合适的回答,由SO网友:lucian 整理而成

事实证明,这很容易,因为可以从slug中检索帖子:

// get the url of the reffering post
$ping_slug = $comment->comment_author_url;
// keep just the slug
$ping_slug = trim(parse_url($ping_slug, PHP_URL_PATH), \'/\');
// get the post from the slug
$ping_post = get_page_by_path($ping_slug, OBJECT, \'post\');
// find the post author data
$ping_author = get_userdata($ping_post->post_author);
// display the author name
echo $ping_author->display_name;
// so on, so forth
更新时间:

我最终完全跳过了xmlrpc api,因为它只对内部链接不太理想,因为我们一直都可以直接访问/只重用查找链接的wp组件。只需自定义字段,就可以更轻松地完成这项工作,但重新调整注释用途的好处是,可以使用已知接口编辑或简单地删除他们不想在帖子中删除的链接。此外,您可以选择不实际显示pingback,但在注释管理部分跟踪内部链接仍然很有用。

因此,函数的运行方式如下:

add_action(\'post_updated\', \'my_cute_internal_pings\');
function my_cute_internal_pings() {
global $post;

// defining or initializing variables
$title = get_the_title($post->ID);
$url = get_the_permalink($post->ID);
$content = get_the_content($post->ID);
$postid = get_the_ID($post->ID);
$links = array();

// this is the function wordpress uses to look for links
$links = wp_extract_urls($content);

// cleaning protocols and whatever – this is for a non-www domani so make sure you make adjustments if needed
$protocols = array(\'http://\', \'https://\', \'http://www.\', \'https://www.\', \'www.\');
$domain = str_replace($protocols, \'\', get_bloginfo(\'url\'));

// here it goes
$allpings = array();
foreach ($links as $key => $link) {

// remove links to other sites
if(!strpos($link, $domain)) {  unset($links[$key]);  }

// keep slug only so you can search for the target post in the database
$parts = explode(\'/\', $link);
$link = implode(\'/\', array_slice($parts, 3, 1));

// now find the post
$postobject = get_page_by_path( $link, OBJECT, array( \'post\' ) );

// and make a nice array of ids
$allpings[] = $postobject->ID;
}

// (a list of sent pings should be kept so you don\'t send them on every update – details below)

// get the previous* list from the custom field
$oldpings = array();
$oldpings = get_post_meta($postid, \'pings\', $single = true);

check if there\'s something new
$newpings = array_diff($allpings,$oldpings);

// now rebuild a complete list of articles mentioned...
$fullpings = array_unique(array_merge($oldpings,$newpings));

and store it store in *a/the custom field
update_post_meta($postid, \'pings\', $fullpings );

// so what should the actual comment be? you can either leave it empty and use the actual excerpt etc for display or...
// you can try and find the exact context on the link

foreach ($newpings as $key => $ping) {
$url = get_permalink($ping);

// this great regex is courtesy of Wiktor Stribiżew https://stackoverflow.com/questions/70433338/regex-php-to-extract-a-sentence-that-contains-a-link
// what it does is to get the entire sentence that contains the link (in $ = output).

$regex = \'~\\b(?:[^.?!]|https?://[^<>\\s"\\\']++)*?\'.preg_quote($url, \'~\').\'(?:[^.?!]|https?://[^<>\\s"\\\']++)*[.?!]*~u\';
if (preg_match_all($regex, $content, $match)){$output = array_map(function($x) {return strip_tags($x);}, $match[0]);}

// (optional) if we think the sentence may be to long, we can set a length limit
$output = substr($output[0], 0, 300);

// now that we have everything we need, we can generate a brand new comment.

wp_insert_comment(array(
// (optional) use this in case you still have regular comments, to differentiate. 
// You may also want to make sure that exclude pingbacks from the comments loop, just search \'separate comments and pingbacks\'.
// If you have pingbacks too, you may want to call it something else... trackbacks or whatever.
    \'comment_type\' => \'pingback\',
// indicating the post we comment on
    \'comment_post_ID\' => $ping,
// (optional) using the post title as the author. I only view this in admin, but you can put whatever here and maybe use it for display too
    \'comment_author\' => $title, 
// here I repurposed the e-mail field, which is not needed for pingbacks
    \'comment_author_email\' => $postid, 
// here we put the url of the post, again mostly for admin display
    \'comment_author_url\' => $url, 
// and this is the content that we got with that regex
    \'comment_content\' => $output
    ));
    }
}
现在,要实际显示pingback,请将其添加到注释模板中

$comments = get_comments(array(\'post_id\' => $post->ID));
if ( $comments ) { 

// here you can do an `echo \'[Posts that link here]\';` or whatever for the title

// getting the \'comments\' newest first
foreach (array_reverse($comments) as $comment) {

// now we get the post id for each originating article
$post = get_post($comment->comment_author_email);

// get everything there is about that article
setup_postdata($post);
global $post;

// if the comment has the content found by that regex, replace the article excerpt on-the-fly
if($comment->comment_content) {$post->post_excerpt = \'[…] <q>\' . $comment->comment_content . \'</q> […]\';}

// [here goes your template for excerpts with all the usual bells and whistles / title, link, thumbnail, taxonomies, actual (not \'comment\') date, (replaced) excerpt etc. ]

// prepare to start over with the nest comment
wp_reset_postdata();
    } 
}