给WordPress添加个额外的古腾堡编辑器

在WordPress中添加一个额外的古腾堡编辑器(Gutenberg Editor)实例可以通过自定义代码实现。以下是一个基本的步骤指南,帮助你在WordPress中添加第二个古腾堡编辑器实例。

步骤 1: 创建一个自定义插件

首先,创建一个自定义插件来包含你的代码。你可以在 wp-content/plugins 目录下创建一个新的文件夹,并在其中创建一个PHP文件。

例如,创建一个名为 custom-gutenberg-editor.php 的文件,并添加以下代码:

复制代码
<?php
/*
Plugin Name: Custom Gutenberg Editor
Description: Adds an additional Gutenberg editor instance.
Version: 1.0
Author: Your Name
*/

function custom_gutenberg_editor_enqueue_scripts() {
    wp_enqueue_script('custom-gutenberg-editor', plugins_url('/js/custom-gutenberg-editor.js', __FILE__), array('wp-blocks', 'wp-element', 'wp-editor'), null, true);
}

add_action('enqueue_block_editor_assets', 'custom_gutenberg_editor_enqueue_scripts');

function custom_gutenberg_editor_init() {
    register_post_type('custom_post_type', array(
        'public' => true,
        'label' => 'Custom Post Type',
        'supports' => array('title', 'editor'),
    ));
}

add_action('init', 'custom_gutenberg_editor_init');

步骤 2: 创建自定义JavaScript文件

在你的插件文件夹中创建一个 js 目录,并在其中创建一个名为 custom-gutenberg-editor.js 的文件。添加以下代码:

复制代码
(function($) {
    var editor = wp.editor.create(document.getElementById('custom-editor'), {
        frame: true,
        title: 'Custom Gutenberg Editor',
        icon: 'admin-post',
        buttons: {
            preview: false,
            fullscreen: true,
            collapse: true,
        },
    });

    editor.on('ready', function() {
        console.log('Custom Gutenberg Editor is ready!');
    });
})(jQuery);

步骤 3: 添加自定义编辑器容器

在你的主题文件中(例如 functions.php 或 header.php),添加一个容器来承载第二个古腾堡编辑器实例。

复制代码
<?php
function custom_gutenberg_editor_container() {
    ?>
    <div id="custom-editor"></div>
    <?php
}
add_action('wp_footer', 'custom_gutenberg_editor_container');
?>

步骤 4: 激活插件

在WordPress后台,导航到"插件"页面,找到你创建的自定义插件并激活它。

步骤 5: 测试

访问你的网站,并检查页面底部是否出现了第二个古腾堡编辑器实例。

注意事项

性能影响:添加额外的编辑器实例可能会对性能产生影响,特别是在低配置的服务器上。

兼容性:确保你的代码与当前使用的WordPress版本兼容。

安全性:在生产环境中使用自定义代码时,请确保进行适当的安全检查和测试。

原文

http://www.dulizhan.cq.cn/jianzhan/42.html

相关推荐
嵌入式学习菌2 小时前
vscode配置ESP32-idf存在的问题
ide·vscode·编辑器
问问计算机3 小时前
vscode analyzing... and its dependencies 耗时过长问题
vscode·编辑器
zephyr_zeng3 小时前
CubeMX项目轻松导入Vscode+EIDE编译
c语言·ide·vscode·stm32·mcu·物联网·编辑器
love混世_魔王4 小时前
VIM经典命令系列之数字递增、递减
linux·编辑器·vim·verilog vim插件·vim使用技巧·vim高效编程
ziyue75754 小时前
vscode和idea配置shfmt格式化shell脚本
ide·vscode·编辑器
ONLYOFFICE4 小时前
如何在ONLYOFFICE协作空间中使用AI智能体?配置应用全攻略
人工智能·ai·编辑器·onlyoffice
好大哥呀4 小时前
Unity3D 2023:游戏开发的全能引擎新选择,新特性实测:XR 适配 + 光照烘焙提速安装
编辑器
徐小夕@趣谈前端4 小时前
flowmix/flow 可视化工作流编辑器, 开源!
编辑器
starvapour5 小时前
替换Ubuntu各种命令的默认编辑器
linux·ubuntu·编辑器
后端研发Marion5 小时前
【深入对比分析三款主流的AI编程CLI编辑器:心流CLI(iFlow CLI)、OpenAI Codex和Claude Code】
编辑器·ai编程·codex·claude code·心流cli