给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

相关推荐
小龙报3 小时前
《VScode搭建教程(附安装包)--- 开启你的编程之旅》
c语言·c++·ide·vscode·单片机·物联网·编辑器
_OP_CHEN15 小时前
Linux网络编程:(七)Vim 编辑器完全指南:从入门到精通的全方位实战教程
linux·运维·服务器·编辑器·vim·linux生态·linux软件
Hody912 天前
【XR开发系列】Unity第一印象:编辑器界面功能布局介绍(六大功能区域介绍)
unity·编辑器·xr
Lucky小小吴2 天前
各大编码编辑器的缓存目录迁移到D盘【未完待续】
缓存·编辑器
李纲明2 天前
开发一个小程序花多少钱
微信小程序·wordpress·wordpress外贸站
言6662 天前
vscode 设置中文-语言
ide·vscode·编辑器
天天向上10243 天前
markdown及vscode中可以的emoji
ide·vscode·编辑器
IT利刃出鞘3 天前
WordPress插件--Markdown编辑器
编辑器
Joyce_JTR3 天前
【vscode】vscode上plantuml安装和使用
ide·vscode·编辑器
Hy行者勇哥3 天前
VSCode 插件开发实战:从零打造「文件模板快建」工具,吃透扩展生态核心技术
ide·vscode·编辑器