register_block_pattern 注冊(cè)自定義Pattern

發(fā)布于:
更新時(shí)間:2025-04-26

本文詳細(xì)介紹了如何使用register_block_pattern函數(shù)在WordPress古騰堡編輯器中注冊(cè)自定義Pattern。內(nèi)容包括函數(shù)參數(shù)、返回值以及一個(gè)完整的使用示例,幫助開(kāi)發(fā)者快速掌握如何創(chuàng)建和注冊(cè)自定義Pattern。

函數(shù)功能

注冊(cè)自定義Pattern 到WordPress古騰堡編輯器。

函數(shù)參數(shù)

參數(shù)數(shù)據(jù)類(lèi)型是否必需描述默認(rèn)值
$pattern_name字符串包含命名空間的Pattern名稱(chēng)無(wú)
$pattern_properties數(shù)組包含Pattern屬性的數(shù)組。無(wú)

$pattern_properties 數(shù)組可以包含以下幾項(xiàng)數(shù)據(jù)。

參數(shù)數(shù)據(jù)類(lèi)型是否必需描述
title字符串模式標(biāo)題
content字符串古騰堡編輯器的內(nèi)容代碼
description字符串Pattern作用的詳細(xì)說(shuō)明
categories字符串Pattern分類(lèi)
keywords字符串Pattern關(guān)鍵詞,用于搜索
viewportWidth字符串在插入時(shí)指定Pattern的寬度

函數(shù)返回值

如果自定義Pattern注冊(cè)成功,返回true,否則為false。

使用示例

下面的示例注冊(cè)了一個(gè)包含兩個(gè)按鈕的自定義Pattern。數(shù)組中 content 的內(nèi)容可以在古騰堡編輯器中編輯好后,切換到古騰堡編輯器的代碼模式,直接復(fù)制代碼過(guò)來(lái)。

register_block_pattern(
    'wpdocs-my-plugin/my-awesome-pattern',
    array(
        'title'       => __( 'Two buttons', 'wpdocs-my-plugin' ),
        'description' => _x( 'Two horizontal buttons, the left button is filled in, and the right button is outlined.', 'Block pattern description', 'wpdocs-my-plugin' ),
        'content'     => "<!-- wp:buttons {\"align\":\"center\"} -->\n<div class=\"wp-block-buttons aligncenter\"><!-- wp:button {\"backgroundColor\":\"very-dark-gray\",\"borderRadius\":0} -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-background has-very-dark-gray-background-color no-border-radius\">" . esc_html__( 'Button One', 'wpdocs-my-plugin' ) . "</a></div>\n<!-- /wp:button -->\n\n<!-- wp:button {\"textColor\":\"very-dark-gray\",\"borderRadius\":0,\"className\":\"is-style-outline\"} -->\n<div class=\"wp-block-button is-style-outline\"><a class=\"wp-block-button__link has-text-color has-very-dark-gray-color no-border-radius\">" . esc_html__( 'Button Two', 'wpdocs-my-plugin' ) . "</a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons -->",
    )
);

我們提供 WordPress主題和插件定制開(kāi)發(fā)服務(wù)

本站長(zhǎng)期承接 WordPress主題、插件、基于 WooCommerce 的商店商城開(kāi)發(fā)業(yè)務(wù)。 我們有 10 年WordPress開(kāi)發(fā)經(jīng)驗(yàn),如果你想 用WordPress開(kāi)發(fā)網(wǎng)站, 請(qǐng)聯(lián)系微信: iwillhappy1314,或郵箱: [email protected] 咨詢。

發(fā)表回復(fù)

您的郵箱地址不會(huì)被公開(kāi)。 必填項(xiàng)已用 * 標(biāo)注

*