juniorrr
Forum Replies Created
-
When you list multiple block names in the
blocksarray, WordPress doesn’t pass any information into thetransform()callback about which one the user clicked. The transform function only receives the source block’s attributes, not the chosen destination block type. So inside that single transform, there’s no reliable way to detect whether the user selected Paragraph or Heading.Because of that, the recommended and most predictable approach is to define separate transform objects, one per target block. That way, each transform has its own logic and explicitly creates the correct block.
For example:
transforms: { to: [ { type: 'block', blocks: [ 'core/paragraph' ], transform: ( attributes ) => { return createBlock( 'core/paragraph', { content: attributes.content, } ); }, }, { type: 'block', blocks: [ 'core/heading' ], transform: ( attributes ) => { return createBlock( 'core/heading', { content: attributes.content, level: 2, } ); }, }, ], },This makes the behavior clear, avoids guesswork, and keeps the code easier to maintain as your block grows.
Think of it a bit like an SRD status check system — you don’t use one generic response for every possible outcome. Each status (approved, pending, declined) has its own clear handling. Block transforms work best the same way: one clear path per target, rather than trying to infer the user’s choice after the fact.