Creating posts
A create form makes one new post per submission, with its CAFE values.
cafe_dynamic_form( [
'id' => 'new-event',
'field_groups' => [ 'group_event_details' ],
'post_type' => 'event', // Default "post".
'post_fields' => [ 'title', 'content', 'excerpt' ],
'post_terms' => [ 'event_category' ],
'post_status' => 'pending', // draft | pending | publish | private
'submit_label' => 'Send event',
'success_message' => 'Thanks! We will review your event.',
'redirect' => home_url( '/events/thank-you/?event={object_id}' ),
] );
WordPress post fields
post_fields value |
Input | Saved as | Check |
|---|---|---|---|
title |
text | post_title (plain text) |
required |
content |
textarea | post_content (HTML allowed by wp_kses_post()) |
- |
excerpt |
textarea | post_excerpt (plain text) |
- |
Post fields are shown above the field groups. Their errors use the keys
post_title and post_terms_{taxonomy}.
A form without any post field still creates its post (WordPress shows it as
"(no title)"). Set a title yourself with
cafe_dynamic_form_post_data.
Categories, tags and other taxonomies
post_terms lists taxonomies registered for the post type. Each shows as a
list of checkboxes (up to 200 terms). Submitted terms must exist, and people
need the taxonomy's assign_terms capability (visitors on a visitor form may
choose them). To pick terms with search, use a Taxonomy field in the
field group instead, see Relationships.
Status
post_status is the status of new posts, default pending. People who may
not publish the post type (publish_posts) always get pending, whatever the
form says, so a form can never be used to publish without that right.
The post author is the signed-in user (0 for visitors).
Who may use a create form
- Signed-in people who may create the post type (
create_posts, e.g. contributors and up for posts), or - people with the form's
capabilitywhen one is set, or - everyone, when
allow_guestsis on (below).
Visitors
'allow_guests' => true,
lets signed-out visitors use the form. Their posts are always pending, and
media fields are never shown to them (they cannot use the media library). The
form has a hidden "honeypot" input that stops simple bots; add a CAPTCHA with
cafe_dynamic_form_after_fields and
cafe_dynamic_form_validate if you need more.
After saving
- No
redirect: the page shows the success message (with AJAX the form is replaced by the message). redirect: the visitor goes there.{object_id}becomes the new post's ID. Only addresses on this site (or hosts allowed with WordPress'sallowed_redirect_hostsfilter) are used; others are ignored.
Sending twice
Every rendered form has a random submission key. Sending the same form twice (double click, "resend" after a lost answer) returns the first result and creates one post, not two. The key is remembered for 10 minutes.