How to customize the output of the block?
-
This block is easy to use for users and flexible to customize for developers. It provides some hooks that allow you to display any kind of meta fields and customize the output to whatever you need. Here are the three most important hooks:
1. The
meta_field_block_get_block_content
hook
With this hook, you can change the output of the block for any kind of field, and it works with any meta-framework. Here is a basic example of how to use it:add_filter( 'meta_field_block_get_block_content', function ( $content, $attributes, $block, $post_id ) { $field_name = $attributes['fieldName'] ?? ''; // Replace your_field_name with your real field name. if ( $field_name === 'your_field_name') { $content = 'new content'; } return $content; }, 10, 4);
2. The
meta_field_block_get_acf_field
hook
This hook is similar to the general one but only works with ACF Fields. The benefit of this hook is that you can see the changes on both the front end and the editor. Here is a basic example of how to use it:add_filter( 'meta_field_block_get_acf_field', function ( $block_content, $post_id, $field, $raw_value ) { $field_name = $field['name'] ?? ''; // Replace your_field_name with your real field name. if ( $field_name === 'your_field_name') { $content = 'new content'; } return $content; }, 10, 4);
3. The
meta_field_block_kses_allowed_html
hook
If your field value contains some special HTML tags or attributes, there is a chance that they are not in the list of allowed tags to display on the front end. To fix that, you can use this hook to add your custom HTML tags or attributes to the allowed HTML tags list. Here is a basic example:add_filter( 'meta_field_block_kses_allowed_html', function ( $allowed_html_tags ) { $allowed_html_tags['tag_1'] = [ 'tag_1_attr_1' => true, 'tag_1_attr_2' => true, ]; $allowed_html_tags['tag_2'] = [ 'tag_2_attr_1' => true, 'tag_2_attr_2' => true, ]; return $allowed_html_tags; }, 10, 4);
You can find many code snippets for the common use cases in the plugin description and easily copy and paste them into your site. If you still need further help, feel free to create a support thread here. I’m more than happy to help.
Thank you all for using this plugin.
Phi.
- The topic ‘How to customize the output of the block?’ is closed to new replies.