Make WordPress Core

Opened 3 months ago

Last modified 10 days ago

#61181 assigned enhancement

Implement a filter to customize output on _block_bindings_post_meta_get_value

Reported by: codersantosh's profile codersantosh Owned by: gziolo's profile gziolo
Milestone: 6.7 Priority: normal
Severity: normal Version: 6.5.3
Component: Editor Keywords: has-patch needs-unit-tests
Focuses: Cc:

Description

Occasionally, developers may prefer to modify the displayed content of a meta rather than showing the exact data. For instance, if the meta key is "prefix_price" and the value is either 0 or unset, they might want to show the text "Free!" instead. This can also be achieved by incorporating another register_block_bindings_source. However, developers often add meta for specific purposes, and integrating a filter can facilitate their use of core/post-meta bindings while customizing the output according to their needs.

Change History (9)

#1 @audrasjb
3 months ago

  • Keywords needs-patch added

This ticket was mentioned in PR #6839 on WordPress/wordpress-develop by @snehapatil02.


6 weeks ago
#2

  • Keywords has-patch added; needs-patch removed

## Ticket: https://core.trac.wordpress.org/ticket/61181

## Description

  • This PR introduces a filter to the _block_bindings_post_meta_get_value function, allowing developers to customize the output of post meta values.
  • This enhancement provides greater flexibility for developers who need to modify the displayed content of a meta value, such as showing "Free!" when the meta value is 0 or unset.

## Changes Made

  • Added Filter Hook: Inserted an apply_filters call before returning the meta value in the _block_bindings_post_meta_get_value function.
  • Filter Name: The filter is named _block_bindings_post_meta_value.

@santosguillamot commented on PR #6839:


5 weeks ago
#3

Thanks for working on this! 🙂 I believe something like this could make sense, but I am not sure how it should be structured. Some of the concerns I have at this point:

  • This filter would only run in the server, which means it would show a different value in the editor. I assume that could be a problem.
  • Does this only apply to post meta or to any binding?
  • If it only applies to post meta, should we add the filter to the bindings logic or directly to get_post_meta / get_metadata function? Does this only apply to bindings or each time you want to retrieve the field?

@gziolo commented on PR #6839:


5 weeks ago
#4

If it only applies to post meta, should we add the filter to the bindings logic or directly to get_post_meta / get_metadata function? Does this only apply to bindings or each time you want to retrieve the field?

There is already an existing filter in get_metadata_raw used internally:

get_{$meta_type}_metadata

@santosguillamot commented on PR #6839:


4 weeks ago
#5

Thinking about this a bit more, I feel more confident that we should probably provide a general filter for bindings that receive the source and the same arguments as get_value_callback. Something similar to the render_block filters. This way, it would be possible to change the value not only of post meta but any other source as well.

@snehapatil02 commented on PR #6839:


4 weeks ago
#6

@SantosGuillamot Introduced a general filter 'block_bindings_source_value' to allow developers to modify the value returned by any block binding source.

Changes Made:

  • Modified the get_value method in the WP_Block_Bindings_Source class to apply the new filter.
  • The filter is applied after the value is retrieved from the source but before it's returned.

Key Benefits:

  1. Flexibility: Works for all binding sources, not just post meta.
  2. Consistency: Functions identically in both the editor and on the front end.
  3. Non-intrusive: It doesn't interfere with existing metadata filters but provides a new layer of customization specific to block bindings.

@bacoords commented on PR #6839:


12 days ago
#7

+1 for this concept. I'll give a real world example and you can tell me if you think this feature would help.

I recently wrote a block variation for the image block to pull in the featured image (yes there's a "featured image" block, but the image block has more options, like the lightbox control). I couldn't use the core/postmeta field where the featured image is stored because it returns an attachment ID, not a URL. So I had to create a custom binding source.

It'd be much less overhead if I could have modified the value sent to the block binding API to return the URL of the featured image instead of the attachment ID. But now I have a custom binding source for one simple meta field AND there's no editor preview on my variation.

Feels like this would be a great example of when this feature would be useful.

@santosguillamot commented on PR #6839:


10 days ago
#8

Thanks for making the changes 🙂 I agree it makes sense to have a filter like this.

I must say that I had this on my to-do list for a while, but I didn't find time yet with the 6.6 release. I'm really sorry for the wait. I would like to take a deeper look and understand better all the use cases.

In the meantime, would it make sense to add some testing? Is this something done for other similar filters?

Feels like this would be a great example of when this feature would be useful.

I'd need to review your implementation, but at first glance, it looks like a good example for this functionality 🙂

#9 @gziolo
10 days ago

  • Component changed from General to Editor
  • Keywords needs-unit-tests added
  • Milestone changed from Awaiting Review to 6.7
  • Owner set to gziolo
  • Status changed from new to assigned
Note: See TracTickets for help on using tickets.