wordpress插件-自定义文章标题颜色3.0.ziphuayulaZIP自定义文章标题颜色3.0.zip 1.02KB 立即下载资源文件列表:ZIP 自定义文章标题颜色3.0.zip 大约有1个文件 自定义文章标题颜色.php 1.95KB 资源介绍: wordpress插件-自定义文章标题颜色3 文章编辑时,右边会出现一个标题颜色设置,可以设置为任何颜色 更新记录:从输入颜色css代码,改为颜色选择器,更方便! <?php /* Plugin Name: 自定义文章标题颜色 Description: 自定义文章标题颜色 Version: 3.0 Author: 大神博客 Author URI: https://ds17.cn/2994.html License: GPLv2 or later License URI: https://ds17.cn/2994.html */ // 添加自定义字段 function custom_title_color_metabox() { add_meta_box('custom_title_color_metabox', '自定义标题颜色', 'custom_title_color_callback', 'post', 'side', 'high'); } add_action('add_meta_boxes', 'custom_title_color_metabox'); // 自定义字段HTML function custom_title_color_callback($post) { $color = get_post_meta($post->ID, '_custom_title_color', true); wp_nonce_field('custom_title_color_nonce', 'custom_title_color_nonce_field'); ?> <p> <label for="custom_title_color">选择标题颜色:</label> <input type="color" id="custom_title_color" name="custom_title_color" value="<?php echo esc_attr($color); ?>"> </p> <?php } // 保存自定义字段 function custom_title_color_save($post_id) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } if (!isset($_POST['custom_title_color_nonce_field']) || !wp_verify_nonce($_POST['custom_title_color_nonce_field'], 'custom_title_color_nonce')) { return; } if (!current_user_can('edit_post', $post_id)) { return; } if (isset($_POST['custom_title_color'])) { update_post_meta($post_id, '_custom_title_color', sanitize_text_field($_POST['custom_title_color'])); } } add_action('save_post', 'custom_title_color_save'); // 输出自定义标题颜色 function custom_title_color_output($title, $id = null) { if (is_singular()) { $color = get_post_meta($id, '_custom_title_color', true); if ($color) { $title = sprintf('<span style="color:%s">%s</span>', esc_attr($color), $title); } } return $title; } add_filter('the_title', 'custom_title_color_output', 10, 2);