WordPress / blueprints-library

32 stars 7 forks source link

Blueprints: Redirect to a post created inside of a Blueprint #52

Open adamziel opened 12 months ago

adamziel commented 12 months ago

There is no convenient way of creating a post inside of a Blueprint and then redirecting the user to the post editor with that post opened.

For example, the Blueprint in block-development-examples#75 had to rely on a hardcoded post ID:

{
  "landingPage": "/wp-admin/post.php?post=4&action=edit",
  "steps": [
    // ... other steps ...
    {
      "step": "runPHP",
      "code": "<?php require '/wordpress/wp-load.php'; wp_insert_post( array('post_title'    => 'Test block','post_content'  => '<!-- wp:block-development-examples/basic-esnext-a2ab62 --><div style=\"background-color:#900;color:#fff;padding:20px\" class=\"wp-block-block-development-examples-basic-esnext-a2ab62\">Hello World, step 1 (from the frontend).</div><!-- /wp:block-development-examples/basic-esnext-a2ab62 -->', 'post_status'   => 'publish', 'post_author'   => 1 ) );"
    }
  ]
}

Let's add support for that. How? I'm not sure at this point – it will take some discussions and explorations.

Done is

FYI @dawidurbanski

soderlind commented 11 months ago

Instead of doing a wp_insert_post, I did a wp_update_post. Since I know that post ID 2 exists, I updated it with my content and made it my landing page:

{
    "$schema": "https://playground.wordpress.net/blueprint-schema.json",
    "landingPage": "\/wp-admin\/post.php?post=2&action=edit",
    "preferredVersions": {
        "php": "8.1",
        "wp": "latest"
    },
    "phpExtensionBundles": [
        "kitchen-sink"
    ],
    "features": {
        "networking": true
    },
    "steps": [
        {
            "step": "login",
            "username": "admin",
            "password": "password"
        },
        {
            "step": "installPlugin",
            "pluginZipFile": {
                "resource": "wordpress.org\/plugins",
                "slug": "jobbnorge-block"
            },
            "options": {
                "activate": true
            }
        },
        {
            "step": "runPHP",
            "code": "<?php require '/wordpress/wp-load.php'; wp_update_post( array('ID' => 2, 'post_title'    => 'Jobbnorge','post_content'  => '<!-- wp:dss/jobbnorge {\"employerID\":\"1981,1992,1980,2770,1989,1994,1986,1984,1985,1987,1996,1988,1982,1983,1995,1993,1991\"} /-->' ) );"
        }
    ]
}

You can test it at https://wordpress.org/plugins/jobbnorge-block/

adamziel commented 10 months ago

Nice! Thank you so much for sharing @soderlind!