Created
February 8, 2026 20:03
-
-
Save damiencarbery/d19b61da48eccb104c92b17d13985d87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| * Plugin Name: New badge for Product Collection block | |
| * Description: Insert a 'New' badge after the product price. | |
| * Plugin URI: https://www.kathyisawesome.com/leverage-block-hooks-to-insert-content/ | |
| * Author: Kathy Awesome | |
| * Author URI: https://www.kathyisawesome.com | |
| * Requires Plugins: woocommerce | |
| * Version: 0.1 | |
| */ | |
| defined( 'ABSPATH' ) || exit; | |
| /** | |
| * Register block without a build directory | |
| * | |
| * @see register_block_type() For registering blocks with render callbacks | |
| * @link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/ | |
| */ | |
| function register_inline_flash_block() { | |
| register_block_type( 'custom/flash-text', array( | |
| 'api_version' => 3, | |
| 'title' => __( 'Custom Flash', 'your-text-domain' ), | |
| 'category' => 'text', | |
| 'render_callback' => 'render_custom_flash_block', | |
| ) ); | |
| //error_log( 'register_block_type: '. var_export( $return, true ) ); | |
| } | |
| add_action( 'init', 'register_inline_flash_block' ); | |
| /** | |
| * Block Render Callback Function | |
| * | |
| * @param array $attributes The block attributes. | |
| * @param string $content The block content. | |
| * @param WP_Block $block The block instance. | |
| * @return string The rendered block output. | |
| */ | |
| function render_custom_flash_block( $attributes, $content, $block ) { | |
| error_log( 'rcfb: ' . var_export( $content, true ) ); | |
| global $product; | |
| $product_id = $product->get_id() ?? null; | |
| // Example: Customize output based on context | |
| $output = '<div class="custom-flash-text has-small-font-size" style="text-align: center; color: #e74c3c; font-weight: 600;">'; | |
| //if ( $product_id && !has_term( 'new', 'product_tag', $product_id ) ) { | |
| $output .= '<span class="condition_new">'. esc_html_e( 'New', 'your-text-domain' ) . '</span>'; | |
| //} | |
| $output .= '</div>'; | |
| return $output; | |
| } | |
| /** | |
| * Use block hooks to inject the custom block after the product price block | |
| * | |
| * @param string $hooked_blocks The list of hooked block types. | |
| * @param string $position The relative position of the hooked blocks. Values: 'before', 'after', 'first_child', or 'last_child'. | |
| * @param string $anchor_block The anchor block type. | |
| * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object, or pattern that the anchor block belongs to. | |
| * @return string | |
| */ | |
| function hook_custom_text_after_product_price( $hooked_blocks, $position, $anchor_block, $context ) { | |
| // Check if we're in the 'after' position. | |
| if ( $position !== 'after' ) { | |
| return $hooked_blocks; | |
| } | |
| // Check if the anchor block is the WooCommerce product price block. | |
| if ( $anchor_block !== 'woocommerce/product-price' ) { | |
| return $hooked_blocks; | |
| } | |
| // Add our custom block to be hooked after product price block. | |
| $hooked_blocks[] = 'custom/flash-text'; | |
| error_log( 'hooked_blocks: ' . var_export( $hooked_blocks, true ) ); | |
| return $hooked_blocks; | |
| } | |
| add_filter( 'hooked_block_types', 'hook_custom_text_after_product_price', 10, 4 ); | |
| /*add_filter( 'hooked_block', 'dcwd_hooked_block', 10, 5); | |
| function dcwd_hooked_block( $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ) { | |
| error_log( '$parsed_hooked_block: ' . var_export( $parsed_hooked_block, true ) ); | |
| error_log( '$hooked_block_type: ' . var_export( $hooked_block_type, true ) ); | |
| error_log( '$relative_position: ' . var_export( $relative_position, true ) ); | |
| }*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment