Context: 我使用的插件允许我在整个网站中添加作者框(Simple Author Box), 同时为任何用户提供一种简单的方式来更新其社交媒体链接和个人资料图片。我想我也可以利用这个插件在网站的about部分动态显示团队成员。
然而,我不想为将来不会发布任何帖子的团队成员(设计师、我自己等)提供作者档案,所以我使用了this question 作为禁用特定用户的作者存档的起点,我在那里创建了一些额外的功能,以自动化该功能(作者存档现在根据每个用户发布的帖子数量自动禁用/启用)。
其中一个函数连接到post_updated
, 根据the docs: "E;每年发射一次existing 帖子已更新"E;[增加强调]
下面是函数的代码(请原谅我缺乏良好的实践,我是PHP新手,不是一个有经验的程序员):
/*
* This function does the checks and the actual value update, if needed.
* It\'s called from inside the callback.
*/
function maybe_update_author_archive_status($user_id, $published_posts, $author_archive_disabled) {
if ($published_posts == 0 && $author_archive_disabled != \'on\') {
update_user_meta($user_id, \'_author_archive_disabled\', \'on\');
} elseif ($published_posts != 0 && $author_archive_disabled != \'off\') {
update_user_meta($user_id, \'_author_archive_disabled\', \'off\');
}
}
/*
* The callback itself.
*/
function maybe_update_author_archive_status_on_post_update($post_id, $post_after, $post_before) {
if($post_before->post_status != \'publish\' && $post_after->post_status != \'publish\') {
return;
}
$old_author = $post_before->post_author;
$new_author = $post_after->post_author;
$authors = array($old_author);
/* If the post author has changed, I might need to update both author archive status */
if($new_author != $old_author) {
$authors[] = $new_author;
}
foreach($authors as $author) {
$user_id = intval($author, 10);
$author_archive_disabled = get_user_meta($user_id, \'_author_archive_disabled\', true);
$published_posts = count_user_posts($user_id);
maybe_update_author_archive_status($user_id, $published_posts, $author_archive_disabled);
}
}
add_action(\'post_updated\', \'maybe_update_author_archive_status_on_post_update\', 10, 3);
然而,令我惊讶的是(事实上也很高兴),当我创建并发布一篇新文章时,它也会被激活。谁能解释一下为什么?在什么情况下不会触发此函数?尽管这是我想要的行为,并且一切都按照我想要的方式工作,但这并不是我在阅读文档后所期望的。