DescriptionDescription
This filter is called when the card builder retrieves available blocks and when a card renders custom blocks.
It allows to add your own blocks in the card builder in order to output custom content with your own markup and logic.
ArgumentsArguments
| Argument | Type | Description | 
| $blocks | array | Holds custom block definitions | 
ExamplesExamples
Image blockImage block
The following example demonstrates how to display an image in a block from a custom field that stores the image ID. This code works with ACF or Meta Box image field (not from a repeater/clone/group). It will also work with any custom fields plugin that stores the image ID.
function register_custom_field_image_block( $blocks ) {
	// "custom_field_image_block" corresponds to the block slug.
	$blocks['custom_field_image_block'] = [
		'name'            => 'Custom Field - Image',
		'render_callback' => 'render_custom_field_image_block',
	];
	return $blocks;
}
add_filter( 'wp_grid_builder/blocks', 'register_custom_field_image_block' );
function render_custom_field_image_block() {
	// Object can be a post, term or user.
	$object = wpgb_get_object();
	// If this is not a post (you may change this condition for user or term).
	if ( ! isset( $object->post_type ) ) {
		return;
	}
	// You have to change "custom_field_name" by yours.
	$image_id = get_post_meta( $object->ID, 'custom_field_name', true );
	if ( empty( $image_id ) ) {
		return;
	}
	// You can change the image size "medium_large" to suit your needs.
	$image_url = wp_get_attachment_image_url( $image_id, 'medium_large' );
	if ( empty( $image_url ) ) {
		return;
	}
	printf(
		'<img src="%s" alt="%s" width="100%%" height="250px" style="object-fit:contain">',
		esc_url( $image_url ),
		esc_attr( get_post_meta( $image_id, '_wp_attachment_image_alt', true ) )
	);
}
			Email blockEmail block
The following example demonstrates how to display an email address from a custom field that stores a string.
function register_custom_field_email_block( $blocks ) {
	// "custom_field_email_block" corresponds to the block slug.
	$blocks['custom_field_email_block'] = [
		'name'            => 'Custom Field - Email',
		'render_callback' => 'render_custom_field_email_block',
	];
	return $blocks;
}
add_filter( 'wp_grid_builder/blocks', 'register_custom_field_email_block' );
function render_custom_field_email_block() {
	// Object can be a post, term or user.
	$object = wpgb_get_object();
	// If this is not a post (you may change this condition for user or term).
	if ( ! isset( $object->post_type ) ) {
		return;
	}
	// You have to change "custom_field_name" by yours.
	$email = get_post_meta( $object->ID, 'custom_field_name', true );
	if ( empty( $email ) ) {
		return;
	}
	$email = antispambot( $email );
	printf(
		'<a href="%s">%s</a>',
		esc_url( 'mailto:' . $email ),
		esc_html( $email )
	);
}
			File blockFile block
The following example demonstrates how to download a file from a URL stored in a custom field.
function register_custom_field_file_block( $blocks ) {
	// "custom_field_file_block" corresponds to the block slug.
	$blocks['custom_field_file_block'] = [
		'name'            => 'Custom Field - File',
		'render_callback' => 'render_custom_field_file_block',
	];
	return $blocks;
}
add_filter( 'wp_grid_builder/blocks', 'register_custom_field_file_block', 10, 1 );
function render_custom_field_file_block() {
	// Object can be a post, term or user.
	$object = wpgb_get_object();
	// If this is not a post (you may change this condition for user or term).
	if ( ! isset( $object->post_type ) ) {
		return;
	}
	// You have to change "custom_field_name" by yours.
	$file_url = get_post_meta( $object->ID, 'custom_field_name', true );
	if ( empty( $file_url ) ) {
		return;
	}
	printf(
		'<a href="%s" download="%s">Download file</a>',
		esc_url( $file_url ),
		esc_attr( wp_basename( $file_url ) )
	);
}
			New badge blockNew badge block
The following example demonstrates how to display a “New” badge for WooCommerce products if the product is newer than 30 days.
function register_product_new_badge_block( $blocks ) {
	// "product_new_badge_block" corresponds to the block slug.
	$blocks['product_new_badge_block'] = [
		'name'            => 'Product - New Badge',
		'render_callback' => 'render_product_new_badge_block',
	];
	return $blocks;
}
add_filter( 'wp_grid_builder/blocks', 'register_product_new_badge_block' );
function render_product_new_badge_block() {
	// Object can be a post, term or user.
	$object = wpgb_get_object();
	// If this is not a product post type.
	if (
		! isset( $object->post_type ) ||
		'product' !== $object->post_type ||
		! function_exists( 'wc_get_product' )
	) {
		return;
	}
	$product = wc_get_product( $object->ID );
	if ( empty( $product ) ) {
		return;
	}
	$days_nb = 30; // Number of days the badge is shown.
	$current = ( new WC_DateTime() )->getTimestamp();
	$created = $product->get_date_created()->getTimestamp();
	if ( $current - $created < DAY_IN_SECONDS * $days_nb ) {
		echo 'NEW';
	}
}