Skip to content

feat: Image generation post-sideload modes (featured/attach/insert) #235

@saraichinwag

Description

@saraichinwag

Why

Currently datamachine/generate-image only sets the generated image as a featured image. If a post already has a featured image, the new image lands in the media library unattached. There's no way to add images into post content via the ability.

Proposal

Add a mode parameter to control post-sideload behavior:

Modes

  • featured (default, current behavior) — Set as featured image. Skip if post already has one.
  • insert — Sideload AND insert a <!-- wp:image --> block into post_content.

Placement Algorithm (position parameter)

  • after_intro (default for insert) — After the first paragraph block
  • before_heading — Before the next H2/H3 heading
  • evenly — Distribute evenly through content (for multiple images, insert at equal intervals between blocks)
  • end — Append at the bottom of post content
  • index:N — Insert before block at index N (0-based)

Interface

// Single image insert
wp_get_ability('datamachine/generate-image')->execute([
    'prompt' => 'A colorful parrot in a rainforest',
    'post_id' => 1234,
    'mode' => 'insert',
    'position' => 'after_intro',
]);

// Featured image (default, existing behavior)
wp_get_ability('datamachine/generate-image')->execute([
    'prompt' => 'A colorful parrot in a rainforest',
    'post_id' => 1234,
    // mode defaults to 'featured'
]);

Implementation scope

  1. ImageGenerationAbilities.php — Add mode and position to input schema, pass through to context
  2. ImageGenerationTask.php — Add insertImageInContent() method alongside existing trySetFeaturedImage():
    • Parse post_content into Gutenberg blocks (parse_blocks())
    • Find insertion point based on position algorithm
    • Build <!-- wp:image --> block with attachment data, alt text, proper alignment
    • Serialize blocks back and wp_update_post()
  3. Route by mode — After sideload: featuredtrySetFeaturedImage(), insertinsertImageInContent()

Block format

<!-- wp:image {"id":123,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="URL" alt="ALT" class="wp-image-123"/></figure>
<!-- /wp:image -->

Builds on #229/#234 (standalone post_id support).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions