Result array:
[context] => Array
(
[col-width] => 100%
[postId] => 7
[postType] => page
)
[available_context:protected] => Array
(
[postId] => 7
[postType] => page
[columns] => 1
)
| <?php | |
| /** | |
| * Plugin Name: WPP Column Context | |
| * Version: 0.1 | |
| * Author: Mukesh Panchal | |
| * License: GPL2 | |
| */ | |
| namespace WPP_Column_Context; | |
| use \WP_HTML_Tag_Processor; | |
| // If this file is called directly, abort. | |
| if ( ! defined( 'WPINC' ) ) { | |
| die; | |
| } | |
| add_filter( | |
| 'render_block_context', | |
| function( $context, $block, $parent ) { | |
| // Merge current context into the parent's context. | |
| if ( $parent ) { | |
| $context = array_merge( $parent->context, $context ); | |
| } | |
| if ( 'core/columns' === $block['blockName'] ) { | |
| $context['columns'] = count( $block['innerBlocks'] ); | |
| } | |
| // For demo purposes, this assumes all unspecified columns are equal widths | |
| if ( 'core/column' === $block['blockName'] ) { | |
| $context['col-width'] = $block['attrs']['width'] ?? (string) round((1 / $context['columns']) * 100, 2) . '%'; | |
| } | |
| return $context; | |
| }, | |
| 10, | |
| 3 | |
| ); | |
| function auto_sizes_filter_image_tag( string $content, array $parsed_block, $block ): string { | |
| echo '<pre>'; | |
| print_r( $block->context ); // Return only "col-width" no "columns" | |
| echo '</pre>'; | |
| echo '<pre>'; | |
| print_r( $block->available_context ); // Return null. | |
| echo '</pre>'; | |
| return $content; | |
| } | |
| add_filter( 'render_block_core/image', __NAMESPACE__ . '\\auto_sizes_filter_image_tag', 10, 3 ); |