首页 / 应用软件 / WordPress插件开发教程,打造专属社交媒体分享工具

WordPress插件开发教程,打造专属社交媒体分享工具

WordPress插件开发教程:打造专属社交媒体分享工具

引言:为什么需要自定义社交媒体分享插件?

在当今数字时代,社交媒体分享已成为网站内容传播的关键环节。虽然市面上已有众多社交媒体分享插件,但它们往往功能臃肿、加载缓慢,或缺乏符合特定品牌需求的定制选项。通过开发自己的WordPress社交媒体分享插件,您不仅可以创建轻量级、高性能的分享工具,还能完全控制其外观、功能和行为,确保与网站设计完美融合。

本教程将引导您从零开始开发一个功能完整的WordPress社交媒体分享插件,涵盖从基础架构到高级功能的完整开发流程。无论您是WordPress开发者还是希望扩展网站功能的内容创作者,都能通过本教程掌握插件开发的核心技能。

第一章:WordPress插件开发基础

1.1 WordPress插件架构概述

WordPress插件本质上是一组PHP文件,通过WordPress提供的API与核心系统交互。一个标准的插件至少包含:

  1. 主插件文件:包含插件元信息的PHP文件
  2. 功能文件:实现插件核心逻辑的PHP文件
  3. 资源文件:CSS、JavaScript和图像等资产
  4. 语言文件:用于国际化的翻译文件

1.2 开发环境搭建

在开始开发前,您需要准备以下环境:

  • 本地或远程WordPress安装(建议使用本地环境如XAMPP、MAMP或Local by Flywheel)
  • 代码编辑器(如VS Code、PHPStorm或Sublime Text)
  • 浏览器开发者工具
  • FTP客户端(用于部署到生产环境)

1.3 创建插件基础结构

首先,在WordPress的wp-content/plugins目录中创建一个新文件夹,命名为custom-social-share。在该文件夹中,创建以下基础文件:

custom-social-share/
├── custom-social-share.php (主插件文件)
├── includes/
│   ├── class-social-share-core.php
│   ├── class-social-share-admin.php
│   └── class-social-share-frontend.php
├── assets/
│   ├── css/
│   │   └── style.css
│   └── js/
│       └── script.js
├── languages/ (可选)
└── uninstall.php (可选)

第二章:创建插件主文件

2.1 插件头部信息

打开custom-social-share.php文件,添加以下标准插件头部信息:

<?php
/**
 * Plugin Name: 自定义社交媒体分享工具
 * Plugin URI:  https://yourwebsite.com/custom-social-share
 * Description: 一个轻量级、可定制的社交媒体分享插件,支持主流社交平台
 * Version:     1.0.0
 * Author:      您的姓名
 * Author URI:  https://yourwebsite.com
 * License:     GPL v2 or later
 * Text Domain: custom-social-share
 * Domain Path: /languages
 */

// 防止直接访问
if (!defined('ABSPATH')) {
    exit;
}

// 定义插件常量
define('CSS_PLUGIN_VERSION', '1.0.0');
define('CSS_PLUGIN_PATH', plugin_dir_path(__FILE__));
define('CSS_PLUGIN_URL', plugin_dir_url(__FILE__));
define('CSS_PLUGIN_BASENAME', plugin_basename(__FILE__));

2.2 插件初始化类

在主文件中添加插件初始化类:

/**
 * 自定义社交媒体分享插件主类
 */
class CustomSocialShare {
    
    private static $instance = null;
    
    /**
     * 获取插件实例(单例模式)
     */
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /**
     * 构造函数
     */
    private function __construct() {
        $this->load_dependencies();
        $this->init_hooks();
    }
    
    /**
     * 加载依赖文件
     */
    private function load_dependencies() {
        // 核心功能类
        require_once CSS_PLUGIN_PATH . 'includes/class-social-share-core.php';
        
        // 后台管理类
        if (is_admin()) {
            require_once CSS_PLUGIN_PATH . 'includes/class-social-share-admin.php';
        }
        
        // 前端显示类
        require_once CSS_PLUGIN_PATH . 'includes/class-social-share-frontend.php';
    }
    
    /**
     * 初始化WordPress钩子
     */
    private function init_hooks() {
        // 插件激活/停用钩子
        register_activation_hook(__FILE__, array($this, 'activate'));
        register_deactivation_hook(__FILE__, array($this, 'deactivate'));
        
        // 初始化插件
        add_action('plugins_loaded', array($this, 'init_plugin'));
        
        // 加载文本域
        add_action('init', array($this, 'load_textdomain'));
    }
    
    /**
     * 插件激活时执行
     */
    public function activate() {
        // 创建或更新数据库表(如果需要)
        // 设置默认选项
        $default_options = array(
            'enabled_platforms' => array('facebook', 'twitter', 'linkedin'),
            'display_position' => 'after_content',
            'button_style' => 'rounded',
            'button_size' => 'medium',
            'share_text' => '分享到:',
            'show_count' => false,
        );
        
        if (!get_option('css_plugin_options')) {
            update_option('css_plugin_options', $default_options);
        }
        
        // 设置插件版本
        update_option('css_plugin_version', CSS_PLUGIN_VERSION);
    }
    
    /**
     * 插件停用时执行
     */
    public function deactivate() {
        // 清理临时数据
        // 注意:这里不删除选项,以便重新激活时保留设置
    }
    
    /**
     * 初始化插件
     */
    public function init_plugin() {
        // 初始化核心类
        SocialShareCore::get_instance();
        
        // 初始化前端类
        SocialShareFrontend::get_instance();
        
        // 如果是后台,初始化管理类
        if (is_admin()) {
            SocialShareAdmin::get_instance();
        }
    }
    
    /**
     * 加载文本域(国际化)
     */
    public function load_textdomain() {
        load_plugin_textdomain(
            'custom-social-share',
            false,
            dirname(CSS_PLUGIN_BASENAME) . '/languages'
        );
    }
}

// 启动插件
CustomSocialShare::get_instance();

第三章:核心功能开发

3.1 创建核心功能类

includes/class-social-share-core.php文件中,添加以下内容:

<?php
/**
 * 社交媒体分享核心功能类
 */
class SocialShareCore {
    
    private static $instance = null;
    private $options;
    
    // 支持的社交平台
    private $supported_platforms = array(
        'facebook' => array(
            'name' => 'Facebook',
            'url' => 'https://www.facebook.com/sharer/sharer.php?u={url}&title={title}',
            'icon' => 'facebook-f',
            'color' => '#3b5998'
        ),
        'twitter' => array(
            'name' => 'Twitter',
            'url' => 'https://twitter.com/intent/tweet?url={url}&text={title}',
            'icon' => 'twitter',
            'color' => '#1da1f2'
        ),
        'linkedin' => array(
            'name' => 'LinkedIn',
            'url' => 'https://www.linkedin.com/shareArticle?mini=true&url={url}&title={title}',
            'icon' => 'linkedin-in',
            'color' => '#0077b5'
        ),
        'pinterest' => array(
            'name' => 'Pinterest',
            'url' => 'https://pinterest.com/pin/create/button/?url={url}&description={title}',
            'icon' => 'pinterest-p',
            'color' => '#bd081c'
        ),
        'whatsapp' => array(
            'name' => 'WhatsApp',
            'url' => 'https://api.whatsapp.com/send?text={title} {url}',
            'icon' => 'whatsapp',
            'color' => '#25d366'
        ),
        'telegram' => array(
            'name' => 'Telegram',
            'url' => 'https://t.me/share/url?url={url}&text={title}',
            'icon' => 'telegram-plane',
            'color' => '#0088cc'
        ),
        'reddit' => array(
            'name' => 'Reddit',
            'url' => 'https://reddit.com/submit?url={url}&title={title}',
            'icon' => 'reddit-alien',
            'color' => '#ff4500'
        ),
        'email' => array(
            'name' => 'Email',
            'url' => 'mailto:?subject={title}&body={url}',
            'icon' => 'envelope',
            'color' => '#333333'
        )
    );
    
    /**
     * 获取实例(单例模式)
     */
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /**
     * 构造函数
     */
    private function __construct() {
        $this->options = get_option('css_plugin_options', array());
        $this->init_hooks();
    }
    
    /**
     * 初始化钩子
     */
    private function init_hooks() {
        // 短码支持
        add_shortcode('social_share', array($this, 'shortcode_handler'));
        
        // AJAX处理
        add_action('wp_ajax_css_get_share_count', array($this, 'ajax_get_share_count'));
        add_action('wp_ajax_nopriv_css_get_share_count', array($this, 'ajax_get_share_count'));
    }
    
    /**
     * 获取支持的平台
     */
    public function get_supported_platforms() {
        return $this->supported_platforms;
    }
    
    /**
     * 获取启用的平台
     */
    public function get_enabled_platforms() {
        $enabled = isset($this->options['enabled_platforms']) ? 
                   $this->options['enabled_platforms'] : 
                   array('facebook', 'twitter', 'linkedin');
        
        // 确保启用的平台在支持的平台列表中
        return array_intersect($enabled, array_keys($this->supported_platforms));
    }
    
    /**
     * 获取分享URL
     */
    public function get_share_url($platform, $post_id = null) {
        if (!isset($this->supported_platforms[$platform])) {
            return '#';
        }
        
        if (!$post_id) {
            global $post;
            $post_id = $post->ID;
        }
        
        $post_data = get_post($post_id);
        $title = urlencode(html_entity_decode(get_the_title($post_id), ENT_COMPAT, 'UTF-8'));
        $url = urlencode(get_permalink($post_id));
        
        $share_url = $this->supported_platforms[$platform]['url'];
        $share_url = str_replace('{url}', $url, $share_url);
        $share_url = str_replace('{title}', $title, $share_url);
        
        // 如果是Pinterest,需要添加图片
        if ($platform === 'pinterest') {
            $image_id = get_post_thumbnail_id($post_id);
            if ($image_id) {
                $image_url = wp_get_attachment_image_url($image_id, 'full');
                $share_url .= '&media=' . urlencode($image_url);
            }
        }
        
        return $share_url;
    }
    
    /**
     * 获取分享按钮HTML
     */
    public function get_share_buttons($post_id = null, $args = array()) {
        if (!$post_id) {
            global $post;
            $post_id = $post->ID;
        }
        
        $defaults = array(
            'platforms' => $this->get_enabled_platforms(),
            'style' => isset($this->options['button_style']) ? $this->options['button_style'] : 'rounded',
            'size' => isset($this->options['button_size']) ? $this->options['button_size'] : 'medium',
            'show_text' => true,
            'show_count' => isset($this->options['show_count']) ? $this->options['show_count'] : false,
            'container_class' => 'css-share-container',
            'before_text' => isset($this->options['share_text']) ? $this->options['share_text'] : '分享到:',
        );
        
        $args = wp_parse_args($args, $defaults);
        
        ob_start();
        ?>
        <div class="<?php echo esc_attr($args['container_class']); ?> css-style-<?php echo esc_attr($args['style']); ?> css-size-<?php echo esc_attr($args['size']); ?>">
            <?php if ($args['show_text'] && !empty($args['before_text'])) : ?>
                <span class="css-share-text"><?php echo esc_html($args['before_text']); ?></span>
            <?php endif; ?>
            
            <div class="css-share-buttons">
                <?php foreach ($args['platforms'] as $platform) : 
                    if (!isset($this->supported_platforms[$platform])) continue;
                    
                    $platform_data = $this->supported_platforms[$platform];
                    $share_url = $this->get_share_url($platform, $post_id);
                    $button_class = 'css-share-btn css-' . $platform;
                    
                    // 添加分享计数类
                    if ($args['show_count']) {
                        $button_class .= ' has-count';
                    }
                ?>
                    <a href="<?php echo esc_url($share_url); ?>" 
                       class="<?php echo esc_attr($button_class); ?>" 
                       target="_blank" 
                       rel="noopener noreferrer"
                       data-platform="<?php echo esc_attr($platform); ?>"
                       data-post-id="<?php echo esc_attr($post_id); ?>"
                       title="<?php printf(__('分享到 %s', 'custom-social-share'), $platform_data['name']); ?>"
                       style="background-color: <?php echo esc_attr($platform_data['color']); ?>">
                        <i class="fab fa-<?php echo esc_attr($platform_data['icon']); ?>"></i>
                        <span class="css-platform-name"><?php echo esc_html($platform_data['name']); ?></span>
                        
                        <?php if ($args['show_count']) : ?>
                            <span class="css-share-count" data-platform="<?php echo esc_attr($platform); ?>">
                                <span class="css-count-number">0</span>
                            </span>
                        <?php endif; ?>
                    </a>
                <?php endforeach; ?>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }
    
    /**
     * 短码处理器
     */
    public function shortcode_handler($atts) {
        $atts = shortcode_atts(array(
            'platforms' => '',
            'style' => '',
            'size' => '',
            'show_text' => true,
            'show_count' => false,
            'text' => '',
        ), $atts, 'social_share');
        
        $args = array();
        
        if (!empty($atts['platforms'])) {
            $args['platforms'] = array_map('trim', explode(',', $atts['platforms']));
        }
        
        if (!empty($atts['style'])) {
            $args['style'] = $atts['style'];
        }
        
        if (!empty($atts['size'])) {
            $args['size'] = $atts['size'];
        }
        
        if (isset($atts['show_text'])) {
            $args['show_text'] = filter_var($atts['show_text'], FILTER_VALIDATE_BOOLEAN);
        }
        
        if (isset($atts['show_count'])) {
            $args['show_count'] = filter_var($atts['show_count'], FILTER_VALIDATE_BOOLEAN);
        }
        
        if (!empty($atts['text'])) {
            $args['before_text'] = $atts['text'];
        }
        
        return $this->get_share_buttons(null, $args);
    }
    
    /**
     * AJAX获取分享计数
     */
    public function ajax_get_share_count() {
        // 安全检查
        if (!check_ajax_referer('css_ajax_nonce', 'nonce', false)) {
            wp_die('安全验证失败', 403);
        }
        
        $platform = sanitize_text_field($_POST['platform']);
        $post_id = intval($_POST['post_id']);
        
        if (!$post_id || !isset($this->supported_platforms[$platform])) {
            wp_send_json_error('参数错误');
        }
        
        $share_count = $this->fetch_share_count($platform, $post_id);
        
        wp_send_json_success(array(
            'count' => $share_count,
            'formatted' => $this->format_count($share_count)
        ));
    }
    
    /**
     * 获取分享计数(示例实现)
     */
    private function fetch_share_count($platform, $post_id) {
        $url = get_permalink($post_id);
        $count = 0;
        
        // 注意:实际实现中,许多社交平台已限制或关闭了分享计数API
        // 这里只是一个示例实现
        
        switch ($platform) {
            case 'facebook':
                // Facebook Graph API示例(需要访问令牌)
                // $api_url = "https://graph.facebook.com/?id=" . urlencode($url);
                // $response = wp_remote_get($api_url);
                // 解析响应获取计数
                $count = rand(0, 100); // 模拟数据
                break;
                
            case 'pinterest':
                // Pinterest API示例
                $count = rand(0, 50); // 模拟数据
                break;
                
            default:
                // 其他平台
            $count = rand(0, 30); // 模拟数据
            break;
    }
    
    // 将计数存储到数据库,减少API调用
    $counts = get_post_meta($post_id, '_css_share_counts', true);
    if (!is_array($counts)) {
        $counts = array();
    }
    
    $counts[$platform] = array(
        'count' => $count,
        'updated' => current_time('timestamp')
    );
    
    update_post_meta($post_id, '_css_share_counts', $counts);
    
    return $count;
}

/**
 * 格式化计数显示
 */
private function format_count($count) {
    if ($count >= 1000000) {
        return round($count / 1000000, 1) . 'M';
    } elseif ($count >= 1000) {
        return round($count / 1000, 1) . 'K';
    }
    return $count;
}

}


### 3.2 创建前端显示类

在`includes/class-social-share-frontend.php`文件中,添加以下内容:

<?php
/**

  • 社交媒体分享前端显示类
    */

class SocialShareFrontend {


private static $instance = null;
private $options;

/**
 * 获取实例(单例模式)
 */
public static function get_instance() {
    if (null === self::$instance) {
        self::$instance = new self();
    }
    return self::$instance;
}

/**
 * 构造函数
 */
private function __construct() {
    $this->options = get_option('css_plugin_options', array());
    $this->init_hooks();
}

/**
 * 初始化钩子
 */
private function init_hooks() {
    // 添加分享按钮到内容
    $position = isset($this->options['display_position']) ? 
                $this->options['display_position'] : 'after_content';
    
    switch ($position) {
        case 'before_content':
            add_filter('the_content', array($this, 'add_share_buttons_before_content'), 5);
            break;
        case 'after_content':
            add_filter('the_content', array($this, 'add_share_buttons_after_content'), 99);
            break;
        case 'both':
            add_filter('the_content', array($this, 'add_share_buttons_both'), 99);
            break;
        case 'manual':
            // 仅通过短码或函数调用
            break;
    }
    
    // 添加浮动分享按钮
    if (isset($this->options['enable_floating']) && $this->options['enable_floating']) {
        add_action('wp_footer', array($this, 'add_floating_share_buttons'));
    }
    
    // 注册前端资源
    add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_assets'));
    
    // 添加结构化数据(增强SEO)
    add_action('wp_head', array($this, 'add_structured_data'));
}

/**
 * 在内容前添加分享按钮
 */
public function add_share_buttons_before_content($content) {
    if ($this->should_display_buttons()) {
        $buttons = SocialShareCore::get_instance()->get_share_buttons();
        return $buttons . $content;
    }
    return $content;
}

/**
 * 在内容后添加分享按钮
 */
public function add_share_buttons_after_content($content) {
    if ($this->should_display_buttons()) {
        $buttons = SocialShareCore::get_instance()->get_share_buttons();
        return $content . $buttons;
    }
    return $content;
}

/**
 * 在内容前后都添加分享按钮
 */
public function add_share_buttons_both($content) {
    if ($this->should_display_buttons()) {
        $buttons = SocialShareCore::get_instance()->get_share_buttons();
        return $buttons . $content . $buttons;
    }
    return $content;
}

/**
 * 添加浮动分享按钮
 */
public function add_floating_share_buttons() {
    if (!$this->should_display_buttons()) {
        return;
    }
    
    $position = isset($this->options['floating_position']) ? 
                $this->options['floating_position'] : 'left';
    
    $platforms = isset($this->options['floating_platforms']) ? 
                 $this->options['floating_platforms'] : 
                 SocialShareCore::get_instance()->get_enabled_platforms();
    
    $args = array(
        'platforms' => $platforms,
        'container_class' => 'css-floating-share css-floating-' . $position,
        'show_text' => false,
        'show_count' => false
    );
    
    echo SocialShareCore::get_instance()->get_share_buttons(null, $args);
}

/**
 * 判断是否应该显示分享按钮
 */
private function should_display_buttons() {
    // 仅在前端显示
    if (is_admin()) {
        return false;
    }
    
    // 检查是否在单篇文章/页面
    if (!is_singular()) {
        return false;
    }
    
    // 检查是否被排除的文章类型
    $excluded_types = isset($this->options['excluded_post_types']) ? 
                      $this->options['excluded_post_types'] : array();
    
    if (in_array(get_post_type(), $excluded_types)) {
        return false;
    }
    
    // 检查是否被排除的特定文章/页面
    $excluded_posts = isset($this->options['excluded_posts']) ? 
                      $this->options['excluded_posts'] : array();
    
    if (in_array(get_the_ID(), $excluded_posts)) {
        return false;
    }
    
    return true;
}

/**
 * 注册前端资源
 */
public function enqueue_frontend_assets() {
    // 仅在前端页面加载
    if (is_admin()) {
        return;
    }
    
    // 加载Font Awesome图标库
    wp_enqueue_style(
        'font-awesome',
        'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css',
        array(),
        '5.15.4'
    );
    
    // 加载插件CSS
    wp_enqueue_style(
        'custom-social-share',
        CSS_PLUGIN_URL . 'assets/css/style.css',
        array(),
        CSS_PLUGIN_VERSION
    );
    
    // 加载插件JavaScript
    wp_enqueue_script(
        'custom-social-share',
        CSS_PLUGIN_URL . 'assets/js/script.js',
        array('jquery'),
        CSS_PLUGIN_VERSION,
        true
    );
    
    // 本地化脚本,传递数据到JavaScript
    wp_localize_script('custom-social-share', 'css_ajax_object', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('css_ajax_nonce'),
        'show_count' => isset($this->options['show_count']) ? $this->options['show_count'] : false
    ));
}

/**
 * 添加结构化数据
 */
public function add_structured_data() {
    if (!is_singular()) {
        return;
    }
    
    $post_id = get_the_ID();
    $post_url = get_permalink($post_id);
    $post_title = get_the_title($post_id);
    $post_excerpt = get_the_excerpt($post_id);
    $post_image = get_the_post_thumbnail_url($post_id, 'full');
    
    $structured_data = array(
        '@context' => 'https://schema.org',
        '@type' => 'Article',
        'headline' => $post_title,
        'description' => $post_excerpt,
        'url' => $post_url,
        'mainEntityOfPage' => array(
            '@type' => 'WebPage',
            '@id' => $post_url
        ),
        'publisher' => array(
            '@type' => 'Organization',
            'name' => get_bloginfo('name'),
            'logo' => array(
                '@type' => 'ImageObject',
                'url' => get_site_icon_url()
            )
        ),
        'datePublished' => get_the_date('c', $post_id),
        'dateModified' => get_the_modified_date('c', $post_id),
        'author' => array(
            '@type' => 'Person',
            'name' => get_the_author_meta('display_name', get_post_field('post_author', $post_id))
        )
    );
    
    if ($post_image) {
        $structured_data['image'] = array(
            '@type' => 'ImageObject',
            'url' => $post_image,
            'width' => 1200,
            'height' => 630
        );
    }
    
    echo '<script type="application/ld+json">' . json_encode($structured_data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . '</script>';
}

}


## 第四章:后台管理界面开发

### 4.1 创建管理类

在`includes/class-social-share-admin.php`文件中,添加以下内容:

<?php
/**

  • 社交媒体分享后台管理类
    */

class SocialShareAdmin {


private static $instance = null;
private $options_page;

/**
 * 获取实例(单例模式)
 */
public static function get_instance() {
    if (null === self::$instance) {
        self::$instance = new self();
    }
    return self::$instance;
}

/**
 * 构造函数
 */
private function __construct() {
    $this->init_hooks();
}

/**
 * 初始化钩子
 */
private function init_hooks() {
    // 添加管理菜单
    add_action('admin_menu', array($this, 'add_admin_menu'));
    
    // 注册设置
    add_action('admin_init', array($this, 'register_settings'));
    
    // 添加插件设置链接
    add_filter('plugin_action_links_' . CSS_PLUGIN_BASENAME, array($this, 'add_plugin_action_links'));
    
    // 加载管理资源
    add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_assets'));
    
    // 添加文章/页面元框
    add_action('add_meta_boxes', array($this, 'add_meta_boxes'));
    add_action('save_post', array($this, 'save_meta_box_data'));
}

/**
 * 添加管理菜单
 */
public function add_admin_menu() {
    $this->options_page = add_options_page(
        __('社交媒体分享设置', 'custom-social-share'),
        __('社交分享', 'custom-social-share'),
        'manage_options',
        'custom-social-share',
        array($this, 'render_options_page')
    );
}

/**
 * 渲染设置页面
 */
public function render_options_page() {
    if (!current_user_can('manage_options')) {
        wp_die(__('您没有权限访问此页面。', 'custom-social-share'));
    }
    
    ?>
    <div class="wrap">
        <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
        
        <form action="options.php" method="post">
            <?php
            settings_fields('css_plugin_options');
            do_settings_sections('custom-social-share');
            submit_button(__('保存设置', 'custom-social-share'));
            ?>
        </form>
        
        <div class="css-preview-section">
            <h2><?php _e('按钮预览', 'custom-social-share'); ?></h2>
            <div id="css-button-preview">
                <?php
                $core = SocialShareCore::get_instance();
                echo $core->get_share_buttons(null, array(
                    'show_count' => true
                ));
                ?>
            </div>
            <p class="description"><?php _e('这是当前设置的分享按钮预览。', 'custom-social-share'); ?></p>
        </div>
        
        <div class="css-shortcode-info">
            <h2><?php _e('短码使用', 'custom-social-share'); ?></h2>
            <p><?php _e('您可以使用以下短码在文章或页面中手动插入分享按钮:', 'custom-social-share'); ?></p>
            <code>[social_share]</code>
            <p><?php _e('短码参数:', 'custom-social-share'); ?></p>
            <ul>
                <li><code>platforms</code>: <?php _e('指定平台,用逗号分隔,如:facebook,twitter,linkedin', 'custom-social-share'); ?></li>
                <li><code>style</code>: <?php _e('按钮样式:rounded, square, circle', 'custom-social-share'); ?></li>
                <li><code>size</code>: <?php _e('按钮大小:small, medium, large', 'custom-social-share'); ?></li>
                <li><code>show_text</code>: <?php _e('是否显示文本:true 或 false', 'custom-social-share'); ?></li>
                <li><code>show_count</code>: <?php _e('是否显示分享计数:true 或 false', 'custom-social-share'); ?></li>
                <li><code>text</code>: <?php _e('自定义分享文本', 'custom-social-share'); ?></li>
            </ul>
            <p><?php _e('示例:', 'custom-social-share'); ?> <code>[social_share platforms="facebook,twitter" style="circle" size="large"]</code></p>
        </div>
    </div>
    <?php
}

/**
 * 注册设置
 */
public function register_settings() {
    register_setting(
        'css_plugin_options',
        'css_plugin_options',
        array($this, 'sanitize_options')
    );
    
    // 基本设置部分
    add_settings_section(
        'css_basic_settings',
        __('基本设置', 'custom-social-share'),
        array($this, 'render_basic_settings_section'),
        'custom-social-share'
    );
    
    // 按钮显示设置部分
    add_settings_section(
        'css_display_settings',
        __('显示设置', 'custom-social-share'),
        array($this, 'render_display_settings_section'),
        'custom-social-share'
    );
    
    // 浮动按钮设置部分
    add_settings_section(
        'css_floating_settings',
        __('浮动按钮设置', 'custom-social-share'),
        array($this, 'render_floating_settings_section'),
        'custom-social-share'
    );
    
    // 排除设置部分
    add_settings_section(
        'css_exclude_settings',
        __('排除设置', 'custom-social-share'),
        array($this, 'render_exclude_settings_section'),
        'custom-social-share'
    );
    
    // 添加字段
    $this->add_settings_fields();
}

/**
 * 渲染基本设置部分
 */
public function render_basic_settings_section() {
    echo '<p>' . __('配置社交媒体分享插件的基本选项。', 'custom-social-share') . '</p>';
}

/**
 * 渲染显示设置部分
 */
public function render_display_settings_section() {
    echo '<p>' . __('配置分享按钮的显示方式和外观。', 'custom-social-share') . '</p>';
}

/**
 * 渲染浮动按钮设置部分
 */
public function render_floating_settings_section() {
    echo '<p>' . __('配置浮动分享按钮的选项。', 'custom-social-share') . '</p>';
}

/**
 * 渲染排除设置部分
 */
public function render_exclude_settings_section() {
    echo '<p>' . __('指定不显示分享按钮的文章类型和特定页面。', 'custom-social-share') . '</p>';
}

/**
 * 添加设置字段
 */
private function add_settings_fields() {
    $core = SocialShareCore::get_instance();
    $platforms = $core->get_supported_platforms();
    
    // 启用的平台
    add_settings_field(
        'enabled_platforms',
        __('启用的平台', 'custom-social-share'),
        array($this, 'render_enabled_platforms_field'),
        'custom-social-share',
        'css_basic_settings',
        array('platforms' => $platforms)
    );
    
    // 分享文本
    add_settings_field(
        'share_text',
        __('分享文本', 'custom-social-share'),
        array($this, 'render_share_text_field'),
        'custom-social-share',
        'css_basic_settings'
    );
    
    // 显示位置
    add_settings_field(
        'display_position',
        __('显示位置', 'custom-social-share'),
        array($this, 'render_display_position_field'),
        'custom-social-share',
        'css_display_settings'
    );
    
    // 按钮样式
    add_settings_field(
        'button_style',
        __('按钮样式', 'custom-social-share'),
        array($this, 'render_button_style_field'),
        'custom-social-share',
        'css_display_settings'
    );
    
    // 按钮大小
    add_settings_field(
        'button_size',
        __('按钮大小', 'custom-social-share'),
        array($this, 'render_button_size_field'),
        'custom-social-share',
        'css_display_settings'
    );
    
    // 显示分享计数
    add_settings_field(
        'show_count',
        __('显示分享计数', 'custom-social-share'),
        array($this, 'render_show_count_field'),
        'custom-social-share',
        'css_display_settings'
    );
    
    // 启用浮动按钮
    add_settings_field(
        'enable_floating',
        __('启用浮动按钮', 'custom-social-share'),
        array($this, 'render_enable_floating_field'),
        'custom-social-share',
        'css_floating_settings'
    );
    
    // 浮动按钮位置
    add_settings_field(
        'floating_position',
        __('
本文来自网络,不代表柔性供应链服务中心立场,转载请注明出处:https://mall.org.cn/5057.html

EXCHANGES®作者

上一篇
下一篇

为您推荐

发表回复

联系我们

联系我们

18559313275

在线咨询: QQ交谈

邮箱: vip@exchanges.center

工作时间:周一至周五,9:00-17:30,节假日休息
返回顶部