frozzare / wp-forms

使用代码在WordPress中创建表单

资助包维护!
www.buymeacoffee.com/frozzare

安装量: 2,979

依赖项: 0

建议者: 0

安全性: 0

星级: 11

关注者: 2

分支: 2

公开问题: 3

类型:wordpress-plugin

dev-master 2017-08-23 13:34 UTC

This package is auto-updated.

Last update: 2024-08-26 01:27:54 UTC


README

需要PHP 5.6和WordPress 4.3

使用代码在WordPress中创建表单。

安装

composer require frozzare/wp-forms

示例

// Add custom field.
forms()
    ->add_field( 'custom', function ( $attributes ) {
        return sprintf( '<p><input type="text" name="%s" /></p>', $attributes['name'] );
    } );

// Add form.
forms()
    ->add( 'contact', [
        'name'  => [
            'label' => 'Name',
            'rules' => 'required|max:250'
        ],
        'email'  => [
            'label' => 'Email',
            'type'  => 'email',
            'rules' => 'required|email'
        ],
        'custom' => [
            'label' => 'Custom',
            'type'  => 'custom'
        ],
        'text'   => [
            'label' => 'Text',
            'type'  => 'textarea'
        ],
        'color'  => [
            'label' => 'Select color',
            'type'  => 'select',
            'items' => [
                [
                    'text'  => 'Blue',
                    'value' => 'blue',
                ],
                [
                    'text'  => 'Green',
                    'value' => 'green'
                ]
            ]
        ]
    ] )
    ->button( 'Send' )
    ->save( function ( array $data ) {
        // Do something with the data...

        // Return true if you will save or email the data yourself
        // otherwise false to save in forms data post type.
        return false;
    } );

// Get errors.
$errors = forms()->errors( 'contact ');

// Render form.
forms()
    ->render( 'contact' );

使用Ajax保存表单

示例

$('.form-submit').on('click', function (e) {
    e.preventDefault();

    $.post('/forms-ajax/?action=save&form=contact', $('form').serialize(), function (res) {
        // res.success is true or false
        // res.errors contains all errors if any.
    });
});

验证规则

alpha, alpha_num, array, between, bool, digit, email, float, int,
ip, min, max, numeric, required, size, string, url

贡献

每个人都可以通过补丁、错误修复和新功能贡献。

许可

MIT © Fredrik Forsmo