使用以下代码创建新插件:
class Facebook_Peeker {
private static $facebook_bots = [
\'facebookexternalhit/1.1 (+https://www.facebook.com/externalhit_uatext.php)\',
\'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)\'
];
private $original_posts;
public function make_scheduled_post_public() {
add_filter( \'posts_results\', [ &$this, \'peek_into_private\' ], null, 2 );
}
public function peek_into_private( $posts, &$query ) {
if ( sizeof( $posts ) != 1 ) {
return $posts;
}
$status = get_post_status_object( get_post_status( $posts[0] ) );
if ( $status->public ) {
return $posts;
}
$this->original_posts = $posts;
add_filter( \'the_posts\', [ &$this, \'override_private\' ], null, 2 );
}
public function override_private( $posts, &$query ) {
remove_filter( \'the_posts\', [ &$this, \'peek_into_private\' ], null, 2 );
return $this->original_posts;
}
private function is_facebook_seeking() {
return in_array( $_SERVER[ \'HTTP_USER_AGENT\' ], self::$facebook_bots );
}
public static function init() {
if ( ! self::is_facebook_seeking() ) {
return;
}
$peeker = new Facebook_Peeker();
$peeker->make_scheduled_post_public();
}
}
add_action( \'plugins_loaded\', [ \'Facebook_Peeker\', \'init\' ] );
这允许Facebook机器人查看预定的帖子,但不允许其他公众查看。如果使用其他插件,请记住更新任何相关的缓存设置。
在以下帮助下得出的答案How to make scheduled post preview visible to anyone? 和How to recognize Facebook User-Agent.