register_block_pattern Register a customized Pattern

Posted in:
Update time:2021-08-20

function function

注册自定义Pattern 到WordPress古腾堡编辑器。

function parameter

parametersdata typeRequired or notdescriptivedefault value
$pattern_namestring (computer science)be包含命名空间的Pattern名称not have
$pattern_propertiesarraysbe包含Pattern属性的数组。not have

$pattern_properties 数组可以包含以下几项数据。

parametersdata typeRequired or notdescriptive
titlestring (computer science)be模式标题
contentstring (computer science)be古腾堡编辑器的内容代码
descriptionstring (computer science)cloggedPattern作用的详细说明
categoriesstring (computer science)cloggedPattern分类
keywordsstring (computer science)cloggedPattern关键词,用于搜索
viewportWidthstring (computer science)clogged在插入时指定Pattern的宽度

Function Return Value

如果自定义Pattern注册成功,返回true,否则为false。

usage example

下面的示例注册了一个包含两个按钮的自定义Pattern。数组中 content 的内容可以在古腾堡编辑器中编辑好后,切换到古腾堡编辑器的代码模式,直接复制代码过来。

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 -->",
    )
);

We offer WordPress Themes and Plugins Custom Development Services

This site has long undertaken WordPress themes, plugins, WooCommerce-based store mall development business. We have 10 years of experience in WordPress development, if you want to Developing Websites with WordPress, please contact WeChat: iwillhappy1314 or email: amos@wpcio.com for inquiries.

发表回复

Your email address will not be published. 必填项已用 * 标注

*