• Resolved Gerard Reches

    (@gerardreches)


    Just as previously asked here:

    Is it possible to generate a post-new.php URL that prefills some fields of the new post edit screen?

    Something like:

    /wp-admin/post-new.php?post_type=custom-post-type&title=CustomTitle&slug=custom_slug

    I’m registering using admin notices when a shortcode is being used but it cannot load what it requires because the associated post doesn’t exist yet.

    For example: [post_title slug="custom_slug"]

    (Don’t ask why am I not using ID, just go with it, this is just an example and not the real scenario)

    So when I detect that the shortcode is being used and it cannot load the post title because there is no post yet with the provided slug, I add an admin notice with a link so that the user can either edit the page where the shortcode is used to fix it and a link that allows the user to create the missing post, prefilling the necessary fields (title and slug).

    We assume that the user may not be smart enough to know which slug they need to create, so we need to prefill it for them.

    • This topic was modified 4 months, 3 weeks ago by Gerard Reches.
Viewing 4 replies - 1 through 4 (of 4 total)
  • To my astonishment, I have just realised: yes, you can.

    Example: /wp-admin/post-new.php?post_type=post&post_title=My+Title&content=aaaa

    You can use this to pre-populate the title and content.

    This works with both the Classic Editor and the Block Editor. All you have to do is click on save/publish.

    Thread Starter Gerard Reches

    (@gerardreches)

    Hi @threadi,

    Thanks, that’s indeed useful in some situations.

    However, I’m curious about why the content=aaaa parameter is pre-populating my meta box input (whose name isn’t ‘content’).

    Also, the one I really wanted to pre-populate was the slug. I’ve tried the following parameters but they didn’t work:

    • slug=my-slug
    • post_name=my-slug
    • name=my-slug

    Is there any way to pre-populate the slug? I know it would be automatically generated from the title, so I could use a title whose sanitized version was the slug I want, but I would like to pre-populate the slug so that the users don’t fill it by themselves.

    The names of the parameters are historical. WordPress is over 20 years old and you are dealing with fields that have been around for just as long. They have certainly evolved along with it. That’s why the parameter is called content to fill post_content.

    The slug of a post is saved in post_name. This cannot be influenced in the above-mentioned way. It is generated automatically by WordPress based on the title of the post. Theoretically, you can try to influence this using the wp_insert_post_data hook, but this requires some customised code.

    Thread Starter Gerard Reches

    (@gerardreches)

    Thanks @threadi .

    I’ve opened a core trac ticket and provided a pull request with the patch. I hope there’s no reason not to include it.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.