erikgreasy/wp-advanced-forms

This is my package wp-advanced-forms

v0.1.2 2023-03-01 11:44 UTC

This package is auto-updated.

Last update: 2024-05-29 14:08:50 UTC


README

Latest Version on Packagist

Advanced forms provide a convenient way to create custom forms in WordPress by making typical form tasks like validation easier.

Current batteries included

  • ability to use laravel validation
  • easy way to config your form without hassle of repeating WP action key, ugly WP hooks callbacks for handling forms and more

Installation

You can install the package via composer:

composer require erikgreasy/wp-advanced-forms

Usage

Create your forms classes extending the base FormComponent class. Example form class:

<?php

namespace App\Forms;

use Erikgreasy\WpAdvancedForms\FormComponent;

class ContactForm extends FormComponent
{
    public bool $usesAjax = true;

    public function handleSubmit()
    {
        // This is where we handle our form.
        // We can use provided laravel validator
        $validator = $this->validator->make($_POST, [
            'name' => 'required'
        ]);

        if($validator->fails()) {
            wp_send_json($validator->errors());
        }

        wp_send_json([
            'message' => 'Success'
        ]);
    }

    public function actionName(): string
    {
        return 'contact_form';
    }
}

Register all your forms in functions.php, or in the plugin:

WpAdvancedForms::load([
    ContactForm::class,
]);

Now you can render your forms with single source of truth based in your form class:

<?php
$form = \Erikgreasy\WpAdvancedForms\WpAdvancedForms::getForm(
    \App\Forms\ContactForm::class
);
?>

{!! $form->openForm() !!}
    
    <input type="text" name="name">

    <button>Submit</button>
{!! $form->closeForm() !!}

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

License

The MIT License (MIT). Please see License File for more information.