核心功能:
- 自动获取网站首屏截图
- 一键设置为文章特色图片
- 支持任意网站URL输入
- 智能处理Cookie和弹窗提示
技术特点:
- 基于screenshotapi.net API
- 高质量PNG格式截图
- 自定义截图尺寸(1200×800)
- 自动图片存储和媒体库管理
使用场景:
- 撰写网站评测文章
- 创建网站导航页面
- 分享优惠促销信息
- 制作网站展示列表
使用优势:
- 操作简单:只需输入网址,保存即可自动生成特色图
- 智能处理:自动处理Cookie提示和弹窗
- 截图精准:只截取网站首屏,避免无用内容
- 配置灵活:支持自定义API设置
使用的是 https://screenshotapi.net/ API接口,注册后每个月可以免费使用100次,满了换个API继续!
<?php
/*
Plugin Name: Auto Featured Image from Screenshot
Plugin URI: https://your-website.com
Description: Automatically generate featured image from website screenshot using screenshotapi.net
Version: 1.0
Author: Your Name
Author URI: https://your-website.com
License: GPL2
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
class Auto_Featured_Image {
private $api_key;
private $debug_mode = true; // 调试模式
public function __construct() {
// 初始化
add_action('admin_init', array($this, 'init'));
// 添加设置页面
add_action('admin_menu', array($this, 'add_admin_menu'));
// 注册设置
add_action('admin_init', array($this, 'register_settings'));
// 添加URL输入框
add_action('add_meta_boxes', array($this, 'add_url_meta_box'));
// 保存文章时处理
add_action('save_post', array($this, 'save_url_meta'));
// 添加设置链接
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'add_settings_link'));
}
public function init() {
$this->api_key = get_option('screenshot_api_key');
}
public function add_settings_link($links) {
$settings_link = '<a href="options-general.php?page=screenshot-api-settings">设置</a>';
array_unshift($links, $settings_link);
return $links;
}
public function add_admin_menu() {
add_options_page(
'Screenshot API设置',
'Screenshot API设置',
'manage_options',
'screenshot-api-settings',
array($this, 'settings_page')
);
}
public function register_settings() {
register_setting('screenshot_api_settings', 'screenshot_api_key');
}
public function settings_page() {
?>
<div class="wrap">
<h1>Screenshot API设置</h1>
<form method="post" action="options.php">
<?php
settings_fields('screenshot_api_settings');
do_settings_sections('screenshot_api_settings');
?>
<table class="form-table">
<tr valign="top">
<th scope="row">API Token</th>
<td>
<input type="text" name="screenshot_api_key"
value="<?php echo esc_attr(get_option('screenshot_api_key')); ?>"
class="regular-text" />
<p class="description">
从 <a href="https://screenshotapi.net/" target="_blank">screenshotapi.net</a> 获取API Token
</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function add_url_meta_box() {
add_meta_box(
'website_url_box',
'网站截图URL',
array($this, 'url_meta_box_html'),
'post',
'side',
'high'
);
}
public function url_meta_box_html($post) {
wp_nonce_field('website_url_save', 'website_url_nonce');
$value = get_post_meta($post->ID, '_website_url', true);
?>
<p>
<input type="url" id="website_url" name="website_url"
value="<?php echo esc_attr($value); ?>"
placeholder="https://example.com"
style="width: 100%;" />
</p>
<p class="description">
输入网址后保存文章将自动获取网站截图作为特色图片
</p>
<?php
if ($this->api_key == '') {
echo '<p style="color: red;">请先在设置页面配置API Token</p>';
}
}
public function save_url_meta($post_id) {
// 安全检查
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!isset($_POST['website_url_nonce']) || !wp_verify_nonce($_POST['website_url_nonce'], 'website_url_save')) return;
if (!current_user_can('edit_post', $post_id)) return;
// 保存URL
if (isset($_POST['website_url'])) {
$url = sanitize_text_field($_POST['website_url']);
update_post_meta($post_id, '_website_url', $url);
if (!empty($url) && !has_post_thumbnail($post_id)) {
$this->generate_featured_image($url, $post_id);
}
}
}
private function generate_featured_image($url, $post_id) {
if (empty($this->api_key)) {
$this->add_admin_notice('error', 'Screenshot API Token未设置');
return false;
}
// 处理Cookie和弹窗的JavaScript
$handle_popups_script = '
document.querySelectorAll("[id*=accept],[class*=accept],[id*=Accept],[class*=Accept]").forEach(el => {
if(el.click) el.click();
});
document.querySelectorAll(".cookie-notice,.cookie-banner,.cookie-consent").forEach(el => {
el.style.display = "none";
});
document.querySelectorAll("[class*=cookie],[class*=Cookie]").forEach(el => {
el.style.display = "none";
});
document.querySelectorAll("[class*=gdpr],[class*=GDPR]").forEach(el => {
el.style.display = "none";
});
document.querySelectorAll(".modal,.popup,.overlay").forEach(el => {
el.style.display = "none";
});
';
// 构建API URL
$api_url = add_query_arg(array(
'token' => $this->api_key,
'url' => urlencode($url),
'output' => 'image',
'file_type' => 'png',
'wait_for_event' => 'load',
'delay' => 2000, // 等待2秒
'full_page' => false, // 只截取首屏
'height' => 800, // 设置高度
'width' => 1200, // 设置宽度
'cookies_enabled' => true, // 启用Cookie
'accept_cookies' => true, // 自动接受Cookie
'scripts' => urlencode($handle_popups_script)
), 'https://shot.screenshotapi.net/screenshot');
// 获取截图
$response = wp_remote_get($api_url, array(
'timeout' => 30 // 30秒超时
));
if (is_wp_error($response)) {
$this->add_admin_notice('error', '获取截图失败: ' . $response->get_error_message());
return false;
}
// 获取图片数据
$image_data = wp_remote_retrieve_body($response);
// 检查是否是有效的图片数据
if (strlen($image_data) < 1000) {
$this->add_admin_notice('error', '获取到的图片数据无效');
return false;
}
// 准备文件
$upload_dir = wp_upload_dir();
$filename = 'screenshot-' . sanitize_title($url) . '.png';
$file = $upload_dir['path'] . '/' . $filename;
// 保存图片
if (file_put_contents($file, $image_data) === false) {
$this->add_admin_notice('error', '保存图片失败');
return false;
}
// 准备附件
$file_type = wp_check_filetype($filename, null);
$attachment = array(
'post_mime_type' => $file_type['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
// 插入附件
$attach_id = wp_insert_attachment($attachment, $file, $post_id);
if ($attach_id == 0) {
$this->add_admin_notice('error', '创建附件失败');
return false;
}
// 生成附件元数据
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
// 设置特色图片
set_post_thumbnail($post_id, $attach_id);
$this->add_admin_notice('success', '成功设置特色图片');
return true;
}
private function add_admin_notice($type, $message) {
add_action('admin_notices', function() use ($type, $message) {
?>
<div class="notice notice-<?php echo $type; ?> is-dismissible">
<p><?php echo esc_html($message); ?></p>
</div>
<?php
});
}
}
// 初始化插件
new Auto_Featured_Image();