给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

相关推荐
kooboo china.2 小时前
Tailwind CSS 实战:基于 Kooboo 构建企业官网页面(一)
前端·css·编辑器
小妖6664 小时前
vscode vue文件单行注释失效解决办法
ide·vscode·编辑器
火柴盒zhang5 小时前
websheet之 编辑器
开发语言·前端·javascript·编辑器·spreadsheet·websheet
Geek__19925 小时前
VSCode远程图形化GDB
ide·vscode·编辑器
xx155802862xx7 小时前
vscode 打开csv乱码
ide·vscode·编辑器
静听夜半雨8 小时前
CANoe入门——3、新建LIN工程及LIN DataBase(LDF文件)的创建
网络·数据库·c++·编辑器
信号处理学渣8 小时前
Git命令行中vim的操作
git·编辑器·vim
Magnum Lehar15 小时前
ApophisZerg游戏引擎项目目录展示
人工智能·vscode·编辑器·游戏引擎
设计源18 小时前
尝试通过DeepSeek来优化提高WordPress网站的打开速度,还真有效!
wordpress·deepseek
亦双城的双子娴19 小时前
基于ueditor编辑器的功能开发之重写ueditor的查找和替换功能,支持滚动定位
编辑器