在WordPress中,要添加一個額外的區塊編輯器(通常指的是Gutenberg區塊編輯器中的一個自定義區塊),你需要編寫一些PHP代碼來注冊新的區塊,并可能還需要一些JavaScript來處理前端的邏輯。下面是一個簡單的示例,展示了如何注冊一個自定義的區塊并在Gutenberg編輯器中使用它。

首先,你需要在你的WordPress主題或插件的functions.php文件中注冊新的區塊。下面是一個基本的例子:

// 注冊自定義區塊
function register_custom_block_style() {
    register_block_style(
        'core/paragraph', // 你可以使用任何核心區塊或自定義區塊的名稱
        'custom-style', // 這是新樣式的名稱
        array(
            'name'           => __( 'Custom Style', 'textdomain' ), // 在這里替換為你的文本域
            'label'          => __( 'Custom Style', 'textdomain' ),
            'style_handle'   => 'custom-css-handle', // 你可以在這里鏈接到一個CSS樣式句柄
            'label'          => __( 'Custom Paragraph', 'textdomain' ),
            'style_handle'   => 'name-of-your-style',
            'label'          => __( 'My Custom Paragraph', 'textdomain' ),
            'style'          => array(
                'name'         => 'custom-style',
                'label'        => __( 'Custom Style', 'textdomain' ),
                'style_handle' => 'name-of-your-style',
            ),
        )
    );
}
add_action( 'init', 'register_custom_block_style' );

在這個例子中,我們注冊了一個新的樣式custom-style到core/paragraph區塊。你需要將name-of-your-style替換為你想要應用的CSS類名。

然后,你需要定義這個CSS樣式。你可以在你的主題的style.css文件中添加它,或者在functions.php文件中使用register_style函數來動態地注冊它:

function register_custom_css_style() {
    register_style( 'name-of-your-style', array(
        'name'         => 'Custom Style',
        'label'        => __( 'Custom Style', 'textdomain' ),
        'style_handle' => 'custom-css-handle',
        'label'        => __( 'Custom Style', 'textdomain' ),
        'style'        => array(
            'color'       => '#ff0000', // 紅色文本
            'background' => '#ffffff', // 白色背景
        ),
        'label'        => __( 'Red Text', 'textdomain' ),
        'style'        => array(
            'color' => '#ff0000',
        ),
        'name'         => 'red-text',
        'label'        => __( 'Red Text', 'textdomain' ),
        'style'        => array(
            'color' => '#ff0000',
        ),
    ) );
}
add_action( 'wp_enqueue_scripts', 'register_custom_css_style' );

在這個例子中,我們定義了一個名為red-text的CSS樣式,它將文本顏色設置為紅色。

請注意,上面的代碼示例僅用于說明目的,并且可能需要根據你的具體需求進行調整。例如,你可能需要為自定義區塊添加更多的設置、屬性或邏輯。

此外,為了完整實現自定義區塊編輯器,你可能還需要編寫JavaScript代碼來處理區塊的渲染和交互。這通常涉及到使用WordPress的registerBlockRender和registerBlockStyle API,并可能需要熟悉Gutenberg編輯器的內部結構和API。

由于實現一個完整的自定義區塊編輯器是一個復雜的任務,上述代碼只是起點。如果你不熟悉WordPress和Gutenberg的內部工作,建議查閱WordPress和Gutenberg的官方文檔,以獲取更詳細的指導。