Entry Form

To add an entry form to your website, create a template with a form that posts to the entries/save-entry controller action:

{% macro errorList(errors) %}
  {% if errors %}
    {{ ul(errors, {class: 'errors'}) }}
  {% endif %}
{% endmacro %}

{# If there were any validation errors, an `entry` variable will be 
   passed to the template, which contains the posted values 
   and validation errors. If that’s not set, we’ll default 
   to a new entry. #}
{% set entry = entry ?? create('craft\\elements\\Entry') %}

{# Add `enctype="multipart/form-data"` to `<form>` if you’re 
   uploading files. #}
<form method="post" accept-charset="UTF-8">
  {{ csrfInput() }}
  {{ actionInput('entries/save-entry') }}
  {{ redirectInput('viewentry/{slug}') }}
  {{ hiddenInput('sectionId', '2') }}
  {{ hiddenInput('enabled', '1') }}

  <label for="title">Title</label>
  {{ input('text', 'title', entry.title, {
    id: 'title',
  }) }}
  {{ _self.errorList(entry.getErrors('title')) }}

  <label for="body">Body</label>
  {{ tag('textarea', {
    id: 'body',
    name: 'fields[body]',
    text: entry.body,
  }) }}
  {{ _self.errorList(entry.getErrors('body')) }}

  <button type="submit">Publish</button>
</form>

Be sure to change the sectionId value to the actual ID of the section to which you’d like to save the entry.

Any custom field handles must be provided in a fields[] array like body is above, and you’ll need to specify a siteId if you don’t intend to post to your Primary site.

You can accept anonymous entry submissions using the Guest Entries plugin.

Editing Existing Entries #

You can modify the form to save existing entries by adding an entryId hidden input to the form:

{{ hiddenInput('entryId', entry.id) }}

Applies to Craft CMS 3.