admin管理员组

文章数量:1134247

I would like to create a new block, "Custom Paragraph," based on the "core/paragraph" block but with the color and typography options removed in the inspector controls. I've written the following plugin but upon activation, the "Custom Paragraph" block does not appear in my available block list.

My knowledge of creating a custom block using PHP is limited and any help would be appreciated.

<?php
/*
Plugin Name: Custom Paragraph Block
Plugin URI: /
Description: A custom paragraph block.
Version: 1.0
Author: Your Name
Author URI: /
*/

function custom_paragraph_block_settings( $settings, $blockname ) {
    if ( $blockname !== 'core/paragraph-custom' ) {
        return $settings;
    }

    // Remove 'color' and 'typography' settings from the inspector controls.
    unset( $settings['supports']['color'] );
    unset( $settings['supports']['typography'] );

    return $settings;
}
add_filter( 'register_block_type_args', 'custom_paragraph_block_settings', 10, 2 );

function register_custom_paragraph_block() {
    $args = array(
        'attributes'      => array(),
        'render_callback' => 'render_custom_paragraph_block',
        'supports'        => array(
            'align' => true, 
        ),
    );

    register_block_type( 'core/paragraph-custom', $args );
}
add_action( 'init', 'register_custom_paragraph_block' );



function render_custom_paragraph_block( $attributes ) {
    // Extract the 'content' and 'customAttribute' attributes from the $attributes array.
    $content = isset( $attributes['content'] ) ? $attributes['content'] : '';
    $customAttribute = isset( $attributes['customAttribute'] ) ? $attributes['customAttribute'] : '';

    return '<p class="custom-paragraph">' . wp_kses_post( $content ) . '</p>';
}```

本文标签: pluginsCustom block based off core block using filter