• Continuing from https://www.ads-software.com/support/topic/filter-before-save/

    I am trying to make changes to a reusable post’s content programmatically while saving it.

    I have tried hooking into content_save_pre and a bunch of other filters mentioned in sanitize_post_field https://developer.www.ads-software.com/reference/functions/sanitize_post_field/

    add_filter('edit_post_content', function ($content, $post_id) {
    	error_log('======> edit_content');
    	return $content;
    }, 10, 2);
    
    add_filter('content_edit_pre', function ($content, $post_id) {
    	error_log('======> edit_content');
    	return $content;
    }, 10, 2);
    
    add_filter('edit_post_content', function ($content, $post_id) {
    	error_log('======> edit_post_content');
    	return $content;
    }, 10, 2);
    
    add_filter('pre_post_content', function ($content) {
    	error_log('======> pre_content');
    	return $content;
    });
    
    add_filter('post_content_pre', function ($content) {
    	error_log('======> content_pre');
    	return $content;
    });
    
    add_filter('post_content', function ($content, $post_id, $context) {
    	error_log('======> edit_content');
    	return $content;
    }, 10, 3);
    
    add_filter('post_post_content', function ($content) {
    	error_log('======> post_content');
    	return $content;
    });

    I see nothing in my error log.

    I wonder if I am looking at the right place. Is there a HOOK to filter saving reusable blocks at all?

    Thanks
    Shawn

    • This topic was modified 3 years, 6 months ago by wpshushu.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Anonymous User 14808221

    (@anonymized-14808221)

    Since reusable blocks are just posts, you could try the most obvious hook:
    save_post

    using something like if ( 'wp_block' !== $post->post_type ) { you can check if you are saving a reusable block or else.

    I did not test this but that is how I would approach the issue, before testing the other hooks you mention above.

    Thread Starter wpshushu

    (@wpshushu)

    @bedas

    Thanks.

    save_post is an action hook but I tried it out anyway. Doesn’t trigger.

    add_action( 'save_post', function($post_id, $post, $update) {
    	error_log('====> save_post');
    	if ($post->post_type == 'wp_block') {
    		$post->post_content = myfunc($post->post_content);
    		wp_update_post($post);
    	}
    }, 10, 3);

    At this point, I might have to dig the REST API source. I doubt wp_blocks are treated as posts. Otherwise it would have triggered some HOOK by now.

    • This reply was modified 3 years, 6 months ago by wpshushu.
    • This reply was modified 3 years, 6 months ago by wpshushu.
    Thread Starter wpshushu

    (@wpshushu)

    Anonymous User 14808221

    (@anonymized-14808221)

    Interesting!

    (Reusable) Blocks are definitely posts, they even have their own post edit screens and get saved in the posts database table as posts of type wp_block.

    save_post would then logically be the hook to go when you want to do things when a post is saved, inclusive make changes to the saved post, even if it is an action.
    So my intuition said – use that.

    But as said, I did not try this with (reusable) blocks, so I was not 100% sure if it works.
    Was worth a try.

    Sorry I couldn’t be of better help on this one ??

    • This reply was modified 3 years, 6 months ago by Anonymous User 14808221.
    Thread Starter wpshushu

    (@wpshushu)

    @bedas

    With the all-mighty all hook, I confirmed that there is absolutely no hook triggered when a reusable hook is being saved(updated).

    Oddly when I start creating a new hook by going to /wp-admin/post-new.php?post_type=wp_block a few hooks are triggered – but this is useless because the content field is still empty at this point. When I click the Save Draft or Publish button, there is no hook triggered.

    In my case, I need my code to run whenever a reusable block is saved (whether newly inserted or updated). Since it seems no hooks are triggered in neither of the cases. I had to find workarounds.

    Luckily, by trashing the block the content_save_pre, save_post_wp_block, save_post actions hooks and the pre_post_content filter hook are triggered. They also trigger when I click the “undo” link after the block has been trashed.

    It is tedious of course and I don’t know why somehow block updating falls out of the normal WP hook chain. It’s completely inconsistent and confusing. But that’s as good as I can get for now.

    • This reply was modified 3 years, 6 months ago by wpshushu.
    • This reply was modified 3 years, 6 months ago by wpshushu.

    @wpshushu,

    Please try this code (tested and worked for me):

    add_filter( 'content_save_pre', function ( $value ) {
    	error_log('======> edit_content');
    	return $value;
    });
    

    Be aware that block editor saving happens in front-end context, so don’t wrap it in a conditional as is_admin() or add it in an admin_init hook either.

    – Benny (@blockmeister)

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘filter reusable block updates’ is closed to new replies.