最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Where do I hook to have the server do something in PHP on block attribute change?

programmeradmin5浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

I'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;
}
发布评论

评论列表(0)

  1. 暂无评论