Skip to content

Accept anonymous entry submissions with Craft.

License

Notifications You must be signed in to change notification settings

Pennebaker/guest-entries

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Guest Entries plugin for Craft

This plugin allows you to save guest entries from the front-end of your website.

Requirements

This plugin requires Craft CMS 3.0.0-beta.22 or later.

Installation

To install the plugin, follow these instructions.

  1. Open your terminal and go to your Craft project:

     cd /path/to/project
    
  2. Then tell Composer to load the plugin:

     composer require craftcms/guest-entries
    
  3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Guest Entries.

Settings

From the plugin settings page, you can configure which sections you want to allow guest entry submissions for, as well as the default entry authors and statuses, and whether submissions should be validated before being accepted.

Usage

Your guest entry template can look something like this:

{% macro errorList(errors) %}
    {% if errors %}
        <ul class="errors">
            {% for error in errors %}
                <li>{{ error }}</li>
            {% endfor %}
        </ul>
    {% endif %}
{% endmacro %}

{% from _self import errorList %}

<form method="post" action="" accept-charset="UTF-8">
    {{ csrfInput() }}
    <input type="hidden" name="action" value="guest-entries/save">
    <input type="hidden" name="redirect" value="success">
    <input type="hidden" name="sectionId" value="3">

    <label for="title">Title</label>
    <input id="title" type="text" name="title"
        {%- if entry is defined %} value="{{ entry.title }}"{% endif -%}>
    
    {% if entry is defined %}
        {{ errorList(entry.getErrors('title')) }}
    {% endif %}

    <label for="body">Body</label>
    <textarea id="body" name="fields[body]">
        {%- if entry is defined %}{{ entry.body }}{% endif -%}
    </textarea>
    
    {% if entry is defined %}
        {{ errorList(entry.getErrors('body')) }}
    {% endif %}

    <input type="submit" value="Publish">
</form>

You will need to adjust the hidden sectionId input to point to the section you would like to post guest entries to.

If you have a redirect hidden input, the user will get redirected to it upon successfully saving the entry.

If there is a validation error on the entry, then the page will be reloaded with an entry variable available to it, set to a craft\elements\Entry model representing the submitted entry. You can fetch the posted values from that variable, as well as any validation errors via getErrors(), getFirstError(), or getFirstErrors(). (The name of this variable is configurable via the “Entry Variable Name” setting.)

Submitting via Ajax

If you submit your form via Ajax with an Accept: application/json header, a JSON response will be returned with the following keys:

  • success (boolean) – Whether the entry was saved successfully
  • errors (object) – All of the validation errors indexed by field name (if not saved)
  • id (string) – the entry’s ID (if saved)
  • title (string) – the entry’s title (if saved)
  • authorUsername (string) – the entry’s author’s username (if saved)
  • dateCreated (string) – the entry’s creation date in ISO 8601 format (if saved)
  • dateUpdated (string) – the entry’s update date in ISO 8601 format (if saved)
  • postDate (string, null) – the entry’s post date in ISO 8601 format (if saved and enabled)
  • url (string, null) – the entry’s public URL (if saved, enabled, and in a section where entries have URLs)

The beforeSaveEntry event

Plugins can be notified right before a guest entry is saved using the beforeSaveEntry event. This is also an opportunity to flag the submission as spam, preventing it from getting saved:

use craft\guestentries\controllers\SaveController;
use craft\guestentries\events\SaveEvent;
use yii\base\Event;

// ...

Event::on(SaveController::class, SaveController::EVENT_BEFORE_SAVE_ENTRY, function(SaveEvent $e) {
    // Grab the entry
    $entry = $e->entry;

    $isSpam = // custom spam detection logic...
    
    if (!$isSpam) {
        $e->isSpam = true;
    }
});

The afterSaveEntry event

Plugins can be notified right after a guest entry is saved using the afterSaveEntry event:

use craft\guestentries\controllers\SaveController;
use craft\guestentries\events\SaveEvent;
use yii\base\Event;

// ...

Event::on(SaveController::class, SaveController::EVENT_AFTER_SAVE_ENTRY, function(SaveEvent $e) {
    // Grab the entry
    $entry = $e->entry;
    
    // Was it flagged as spam?
    $isSpam = $e->isSpam;
});

The afterError event

Plugins can be notified right after a submission is determined to be invalid using the afterError event:

use craft\guestentries\controllers\SaveController;
use craft\guestentries\events\SaveEvent;
use yii\base\Event;

// ...

Event::on(SaveController::class, SaveController::EVENT_AFTER_ERROR, function(SaveEvent $e) {
    // Grab the entry
    $entry = $e->entry;

    // Get any validation errors
    $errors = $entry->getErrors();
});

About

Accept anonymous entry submissions with Craft.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 78.2%
  • HTML 21.8%