I am trying to override a WooCommerce product collections block with custom classes, but hitting issues in filters.php
. So I added a test function
function test_function($block_content, $block) {
echo '<script>console.log("Function called");</script>';
return $block_content;
}
add_filter('render_block', 'test_function', 10, 2);
But then I get the same error:
Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, function "test_function" not found or invalid function name in /Users/me/code/imwzv2/wp-includes/class-wp-hook.php:326 Stack trace: #0 /Users/jasperfrumau/code/imwzv2/wp-includes/plugin.php(205):
all app/filters.php
now has is
<?php
/**
* Theme filters.
*/
namespace App;
/**
* Add "… Continued" to the excerpt.
*
* @return string
*/
add_filter('excerpt_more', function () {
return sprintf(' … <a href="%s">%s</a>', get_permalink(), __('Continued', 'sage'));
});
/**
* body_class filter
* @link https://developer.wordpress.org/reference/functions/add_filter/
* @link https://developer.wordpress.org/reference/hooks/body_class/
*/
add_filter( 'body_class', function( $classes ) {
return array_merge( $classes, array( 'bg-white text-gray-600 work-sans leading-normal text-base tracking-normal' ) );
});
// function add_custom_class__product_collection_images($block_content, $block)
// {
// if (is_front_page() && strpos($block_content, 'wc-block-grid__product-image') !== false) {
// // Simple string replacement
// if ( $block['blockName'] === 'woocommerce/product-image' ) {
// $block_content = str_replace('wc-block-grid__product-image', 'wc-block-grid__product-image hover:grow hover:shadow-lg', $block_content);
// }
// }
// if ( 'woocommerce/product-image' === $block['blockName'] &&
// ! empty( $block['attrs']['className'] ) &&
// preg_match( '/\bwc-block-grid__product-image\b/', $block['attrs']['className'] ) ) {
// $block_content = 'wc-block-grid__product-image hover:grow hover:shadow-lg';
// }
// echo '<script>console.dir(' . json_encode($block) . ')</script>';
// // Returns altered $block_content to be rendered
// return $block_content;
// }
// // Add filter to 'woocommerce/product-image' block
// add_filter('render_block', 'add_custom_class__product_collection_images', 10, 2);
function test_function($block_content, $block) {
echo '<script>console.log("Function called");</script>';
return $block_content;
}
add_filter('render_block', 'test_function', 10, 2);
What am I doing wrong here ?