Support » Plugin: Gutenberg » How to hook into post status transitions?

  • Scott Fennell

    (@scofennellgmailcom)


    Hi, I have a plugin that sends an email when a post goes to “Pending Review” status. I’d like to update my plugin so that it works with Gutenberg, but I’m having trouble. Here is what I have tried:

    const { subscribe } = wp.data;
    
    const initialPostStatus = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'status' );
    
    if( 'pending' !== initialPostStatus ) {
    
    	console.log( initialPostStatus );
    
    	const unsubscribe = subscribe( () => {
    
    		const currentPostStatus = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'status' );
    		const currentPostId = wp.data.select( 'core/editor' ).getCurrentPostId();
    
    		if( 'pending' === currentPostStatus ) {
    
    			// do stuff
    			
    		}
    
    	});
    
    }
    

    However, initialPostStatus is always undefined, so my conditional is never met.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Transitioning the post status still makes a change in the database, so the existing PHP code is invoked. Does that existing action/filter mechanism not work anymore?

    Scott Fennell

    (@scofennellgmailcom)

    It seems to me that it does not. My plugin hooks in to these hooks:

    $hooks = array(
       'draft_to_pending',
       'pending_to_publish',
       'publish_to_pending'
    );

    I have this set up as a reduced test case and I AM able to get code to execute on these hooks WITHOUT gutenberg. I am NOT able to get code to execute on these hooks WITH gutenberg.

    I am facing the same issue. I got the code example you used from this issue on github: https://github.com/WordPress/gutenberg/issues/4674#issuecomment-404587928.

    initialPostStatus is also undefined for me. Maybe we are using the code too early or at the wrong place? Do you have any progress on this?

    Scott Fennell

    (@scofennellgmailcom)

    Moderator Marius L. J.

    (@clorith)

    Hiya,

    To use the wp.data module, are you declaring wp-data as a dependency when enqueueing your own JavaScript? This is the most common case for this.

    Scott Fennell

    (@scofennellgmailcom)

    Thank you!

    I can try that. How do you know this? To what documentation may I refer, in order to also know it?

    Thank you again, I will try it!

    Moderator Marius L. J.

    (@clorith)

    It’s based off the normal WordPress enqueue flow to always explicitly declare any dependencies, as load orders etc may change.

    As for this specific instance, it’s because I saw others having the same problem here on the forums šŸ™‚

    Scott Fennell

    (@scofennellgmailcom)

    Thanks!

    I understand general WordPress enqueueing. What I don’t understand is how developers are expected to know that the wp-data script is a dependency for using the wp.data module. I’m not seeing that in any documentation anywhere. I’ve suggested a change: https://github.com/WordPress/gutenberg/compare/master…scofennell:patch-1

    • This reply was modified 1 year, 11 months ago by Scott Fennell.
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to hook into post status transitions?’ is closed to new replies.