WordPress智能摘要插件

步骤 1:创建插件的基础文件

首先,在wp-content/plugins/目录下创建一个新文件夹,如chatglm-summary-plugin。然后在该文件夹中创建一个PHP文件,比如chatglm-summary-plugin.php

步骤 2:编写插件的主文件

chatglm-summary-plugin.php中,添加以下代码:

<?php
/**
 * Plugin Name: ChatGLM Summary Plugin
 * Description: 通过ChatGLM的API对文章正文生成一个100字左右的摘要并插入到文章开头。
 * Version: 1.0
 * Author: Your Name
 */

if (!defined('ABSPATH')) {
    exit; // 避免直接访问文件
}

// 插件激活时的操作
function chatglm_summary_plugin_activate() {
    // 添加默认设置项
    add_option('chatglm_api_key', '');
}
register_activation_hook(__FILE__, 'chatglm_summary_plugin_activate');

// 插件停用时的操作
function chatglm_summary_plugin_deactivate() {
    // 删除设置项
    delete_option('chatglm_api_key');
}
register_deactivation_hook(__FILE__, 'chatglm_summary_plugin_deactivate');

// 在管理菜单中添加设置页面
function chatglm_summary_plugin_menu() {
    add_options_page(
        'ChatGLM Summary Plugin Settings',
        'ChatGLM Summary',
        'manage_options',
        'chatglm-summary-plugin',
        'chatglm_summary_plugin_settings_page'
    );
}
add_action('admin_menu', 'chatglm_summary_plugin_menu');

// 设置页面内容
function chatglm_summary_plugin_settings_page() {
    ?>
    <div class="wrap">
        <h1>ChatGLM Summary Plugin Settings</h1>
        <form method="post" action="options.php">
            <?php
            settings_fields('chatglm_summary_plugin_options_group');
            do_settings_sections('chatglm-summary-plugin');
            submit_button();
            ?>
        </form>
    </div>
    <?php
}

// 注册设置
function chatglm_summary_plugin_settings_init() {
    register_setting('chatglm_summary_plugin_options_group', 'chatglm_api_key');

    add_settings_section(
        'chatglm_summary_plugin_settings_section',
        'API Settings',
        null,
        'chatglm-summary-plugin'
    );

    add_settings_field(
        'chatglm_api_key',
        'ChatGLM API Key',
        'chatglm_summary_plugin_api_key_render',
        'chatglm-summary-plugin',
        'chatglm_summary_plugin_settings_section'
    );
}
add_action('admin_init', 'chatglm_summary_plugin_settings_init');

// 渲染API Key输入框
function chatglm_summary_plugin_api_key_render() {
    $api_key = get_option('chatglm_api_key');
    ?>
    <input type="text" name="chatglm_api_key" value="<?php echo esc_attr($api_key); ?>" size="50">
    <?php
}

// 在保存文章时生成摘要并插入文章开头
function chatglm_summary_generate($post_id) {
    // 检查是否为自动保存,避免重复操作
    if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
        return;
    }

    $post = get_post($post_id);
    $content = $post->post_content;

    // API Key
    $api_key = get_option('chatglm_api_key');

    if (empty($api_key)) {
        return;
    }

    // 调用ChatGLM API生成摘要
    $summary = chatglm_get_summary($content, $api_key);

    if ($summary) {
        // 在文章开头插入摘要
        $new_content = "<p><strong>摘要:</strong> $summary</p>" . $content;

        // 更新文章内容
        wp_update_post([
            'ID' => $post_id,
            'post_content' => $new_content,
        ]);
    }
}
add_action('save_post', 'chatglm_summary_generate');

// 调用ChatGLM API的函数
function chatglm_get_summary($content, $api_key) {
    $api_url = "https://api.chatglm.com/v1/summary"; // 这里请替换为实际的ChatGLM API地址

    $response = wp_remote_post($api_url, [
        'headers' => [
            'Authorization' => 'Bearer ' . $api_key,
            'Content-Type' => 'application/json',
        ],
        'body' => json_encode([
            'text' => $content,
            'max_length' => 100, // 生成100字左右的摘要
        ]),
    ]);

    if (is_wp_error($response)) {
        return false;
    }

    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);

    return isset($data['summary']) ? $data['summary'] : false;
}

步骤 3:说明

  1. 插件结构:该插件会在后台的“设置”菜单下添加一个“ChatGLM Summary”选项,用户可以在这里输入ChatGLM的API密钥。
  2. API调用:在保存文章时,插件会调用ChatGLM的API来生成一个100字左右的摘要,并将摘要插入到文章开头。
  3. API URL:请确保替换代码中的https://api.chatglm.com/v1/summary为实际的ChatGLM API的URL。
  4. 错误处理:在实际使用中,应添加更多的错误处理,例如API请求失败时的处理,确保不会影响文章的正常保存。

步骤 4:安装和启用插件

将插件上传到WordPress的插件目录(wp-content/plugins/)后,在WordPress后台的插件页面中激活该插件。激活后,在“设置”菜单中会出现“ChatGLM Summary”选项,用户可以在那里输入ChatGLM的API密钥。

步骤 5:测试和调试

创建或编辑一篇文章并保存,查看文章开头是否成功插入了摘要。如果没有出现摘要,请检查API请求是否成功,以及API密钥是否正确。

这样,你就完成了一个简单的WordPress插件,它能够通过ChatGLM的API自动生成文章摘要并插入到文章的开头。

0 0 投票数
Article Rating
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x