I'm creating a new block plugin, and I'd like the server to do some stuff when certain attributes change. I'm thinking I'm looking for something like
function my_attribute_changed ( $block_ID, $attribute, $value ) {
// do some stuff
}
add_action('gutenberg-attribute-change', 'my_attribute_changed', 10, 3);
but I'm not seeing any such thing in the docs, and am thinking maybe I'm barking up the wrong tree, so to speak.
Context I imagine there are lots of reasons one may want to do such a thing, but in this particular instance, the block's edit function has an attribute that is a URL that points to a 3rd party API. My plan is set a wp_cron to grab the data at regular intervals, and store it in transient. Then the block's save function will grab that transient data via AJAX.
I'm creating a new block plugin, and I'd like the server to do some stuff when certain attributes change. I'm thinking I'm looking for something like
function my_attribute_changed ( $block_ID, $attribute, $value ) {
// do some stuff
}
add_action('gutenberg-attribute-change', 'my_attribute_changed', 10, 3);
but I'm not seeing any such thing in the docs, and am thinking maybe I'm barking up the wrong tree, so to speak.
Context I imagine there are lots of reasons one may want to do such a thing, but in this particular instance, the block's edit function has an attribute that is a URL that points to a 3rd party API. My plan is set a wp_cron to grab the data at regular intervals, and store it in transient. Then the block's save function will grab that transient data via AJAX.
Share Improve this question asked Oct 4, 2020 at 10:00 philolegeinphilolegein 2832 silver badges12 bronze badges1 Answer
Reset to default 0I'm not sure that I understood exactly your goal, but check if hooking to render_block does what you want, as it will modify the block's output before it gets rendered in the frontend. Usage example:
add_filter( 'render_block', 'my_dynamic_data', 10, 2 );
function my_dynamic_data( $block_content, $block ) {
if('my_value' === $block['attrs']['myAttribute'] ) {
// Do something
}
return $block_content;
}