自学内容网 自学内容网

给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


原文地址:https://blog.csdn.net/jianzhanyes/article/details/143001740

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!