add support for CUSTOM ATTRIBUTES

[ad_1]

Hi @aryanrajseo,

I’m assuming you mean adding custom HTML attributes to the frontend markup? If so, you can extend the block manually using the render_block and the WP_HTML_Tag_Processor. The following example adds a custom attribute to the outer wrapper of each Icon block. From there, you can modify as needed.

// Add the filter to modify the outermost 'icon-block' block markup
add_filter( 'render_block', 'modify_icon_block_markup', 10, 2 ;

function modify_icon_block_markup( $block_content, $block ) {
// Check if the block is 'icon-block'
if ( $block['blockName'] === 'outermost/icon-block' ) {
// Load the WP_HTML_Tag_Processor class
if ( class_exists('WP_HTML_Tag_Processor' ) ) {
// Initialize the tag processor
$processor = new WP_HTML_Tag_Processor( $block_content );
// Find the first outermost wrapper tag
if ( $processor->next_tag() ) {
// Add custom attribute to the tag
$processor->set_attribute( 'custom-attribute', 'custom-value' );
// Get the modified content
$block_content = $processor->get_updated_html();
}
}
}

return $block_content;
}

Since I would consider this a more advanced implementation of the Icon block, and since it is possible with standard block extensibility mechanisms in WordPress itself, this is not something that I am going to add to the block.

Let me know if I can help further.

Best,
Nick

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer