<?php
/*
Plugin Name: 文章图片水印插件
Plugin URI: https://ds17.cn/2711.html
Description: 为文章中的图片自动添加水印遮罩层,而不修改原始图片。
Version: 1.0.0
Author: Your Name
Author URI: https://ds17.cn/2711.html
License: GPLv2 or later
License URI: https://ds17.cn/2711.html
*/
function my_watermark_plugin_add_meta_box() {
add_meta_box('my_watermark_plugin_meta_box', '文章图片水印设置', 'my_watermark_plugin_meta_box_callback', 'post', 'side', 'high');
}
add_action('add_meta_boxes', 'my_watermark_plugin_add_meta_box');
function my_watermark_plugin_meta_box_callback($post) {
$watermark_number = get_post_meta($post->ID, 'my_watermark_plugin_watermark_number', true);
$watermark_position = get_post_meta($post->ID, 'my_watermark_plugin_watermark_position', true);
?>
<div class="inside">
<p>
<label for="my_watermark_plugin_watermark_number">水印编号:</label>
<input type="text" name="my_watermark_plugin_watermark_number" id="my_watermark_plugin_watermark_number" value="<?php echo esc_attr($watermark_number); ?>" />
</p>
<p>
<label for="my_watermark_plugin_watermark_position">水印位置:</label>
<select name="my_watermark_plugin_watermark_position" id="my_watermark_plugin_watermark_position">
<option value="center" <?php selected($watermark_position, 'center'); ?>>居中</option>
<option value="top-left" <?php selected($watermark_position, 'top-left'); ?>>左上</option>
<option value="bottom-left" <?php selected($watermark_position, 'bottom-left'); ?>>左下</option>
</select>
</p>
</div>
<?php
}
function my_watermark_plugin_save_post($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
if (isset($_POST['my_watermark_plugin_watermark_number'])) {
update_post_meta($post_id, 'my_watermark_plugin_watermark_number', sanitize_text_field($_POST['my_watermark_plugin_watermark_number']));
}
if (isset($_POST['my_watermark_plugin_watermark_position'])) {
update_post_meta($post_id, 'my_watermark_plugin_watermark_position', sanitize_text_field($_POST['my_watermark_plugin_watermark_position']));
}
}
add_action('save_post', 'my_watermark_plugin_save_post');
function my_watermark_plugin_add_watermark($content) {
global $post;
// 获取自定义水印编号
$watermark_number = get_post_meta($post->ID, 'my_watermark_plugin_watermark_number', true);
// 获取自定义水印位置
$watermark_position = get_post_meta($post->ID, 'my_watermark_plugin_watermark_position', true);
// 定义水印遮罩层的样式
$watermark_style = '
<style>
.my-watermark {
position: relative;
}
.my-watermark::after {
content: "编号:' . esc_attr($watermark_number) . '";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
font-size: 24px;
font-weight: bold;
text-align: center;
line-height: 2;
display: flex;
justify-content: center;
align-items: center;
opacity: 0.5;
pointer-events: none;
}
</style>
';
// 根据水印位置设置遮罩层的样式
switch ($watermark_position) {
case 'top-left':
$watermark_style = str_replace('top: 0;', 'top: 0;', $watermark_style);
$watermark_style = str_replace('left: 0;', 'left: 0;', $watermark_style);
break;
case 'bottom-left':
$watermark_style = str_replace('top: 0;', 'bottom: 0;', $watermark_style);
$watermark_style = str_replace('left: 0;', 'left: 0;', $watermark_style);
break;
default:
$watermark_style = str_replace('top: 0;', 'top: 50%;', $watermark_style);
$watermark_style = str_replace('left: 0;', 'left: 50%;', $watermark_style);
break;
}
// 为文章中的图片添加水印遮罩层
$content = preg_replace('/<img([^>]+)>/i', '<div class="my-watermark" style="display: inline-block;"><img$1></div>', $content);
return $watermark_style . $content;
}
add_filter('the_content', 'my_watermark_plugin_add_watermark');