[WordPress] 外掛分享: Motiforms

首頁外掛目錄 › Motiforms
10+
安裝啟用
尚無評分
3045 天前
最後更新
問題解決
WordPress 3.8+ v0.1.0 上架:2017-11-09

內容簡介

警告

如果您不是開發人員,這個外掛不適合您。 Motiforms 不提供任何 WordPress 管理介面以建立表單。

功能

處理表單邏輯
欄位清洗
欄位驗證
內建 HTML 渲染輔助工具
彈性度高
基於進階的 Symfony 框架

開始使用

要建立簡單的聯繫表單,請將以下程式碼貼到您的 functions.php 文件中。並將 [contact] 簡碼貼到您的聯繫頁面。

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class ContactForm {

/**
* 表單實體
*
* FormType
*/
private $form;

/**
* ContacForm 建構函式
*
* @return ContacForm
*/
public function __construct() {

$this->define_hooks();
}

/**
* 建立與處理聯繫表單
*
* 這個方法會在 WP 動作鉤執行。
* 它只會在擁有聯繫簡碼的頁面執行。
*
* @return void
*/
public function controller() {

global $post;

// 檢查是否為頁面且頁面內容有簡碼
if ( is_page() && has_shortcode( $post->post_content, 'contact' ) ) {

$factory = mf_get_factory();

// 建立表單
$this->form = $factory->create();

// 將欄位加入表單
$this->form->add( 'full_name', TextType::class );
$this->form->add( 'email', EmailType::class );
$this->form->add( 'message', TextareaType::class );
$this->form->add( 'submit', SubmitType::class );

// 取得請求物件
$request = mf_get_request();

// 處理請求
$this->form->handleRequest( $request );

// 檢查表單是否有效
if ( $this->form->isSubmitted() && $this->form->isValid() ) {

// 從表單取得資料
$data = $this->form->getData();

// 定義篩選器
$filters = array(
'full_name' => FILTER_SANITIZE_STRING,
'email' => FILTER_SANITIZE_STRING | FILTER_SANITIZE_EMAIL,
'message' => FILTER_SANITIZE_STRING,
);

// 欄位清洗
$sanitized_data = filter_var_array( $data, $filters );

// 處理表單資料的動作,例如發送電子郵件

// 以成功參數將使用者重新導向以防止重複提交表單
wp_safe_redirect( $this->get_redirect_url() );
}
}
}

/**
* 渲染聯繫表單。
*
* 這個方法會在聯繫簡碼執行。
*
* @return string
*/
public function render() {

$success = filter_input( INPUT_GET, 'success', FILTER_SANITIZE_NUMBER_INT );

if( '1' === $success ) {
return sprintf('<h2>%s</h2>', __('非常感謝您提交表單,我們將會盡快聯繫您。') ) ;
}

return $this->form->createView()->render();
}
}

外掛標籤

開發者團隊

⬇ 下載最新版 (v0.1.0) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「Motiforms」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

WARNING
If you are not developer this plugin is not for you. Motiforms do not provide any WordPress admin interface to creating forms.
Features

Handle form logic
Field sanitization
Field validation
Built in html rendering helpers
Flexibility
Based on advanced Symfony framework

Get started
To create simple contact form paste code bellow to your functions.php file. And paste [contact] shortcode to your contact page.
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class ContactForm {

/**
* Form instance
*
* FormType
*/
private $form;

/**
* ContacForm constructor
*
* @return ContacForm
*/
public function __construct() {

$this->define_hooks();
}

/**
* Create and process contact form
*
* This method is executed by wp action hook.
* It will be executed only on page which has contact
* shortcode.
*
* @return void
*/
public function controller() {

global $post;

// Check if current view is page and page has content shortcode
if ( is_page() && has_shortcode( $post->post_content, 'contact' ) ) {

$factory = mf_get_factory();

// Create form
$this->form = $factory->create();

// Add fields to form
$this->form->add( 'full_name', TextType::class );
$this->form->add( 'email', EmailType::class );
$this->form->add( 'message', TextareaType::class );
$this->form->add( 'submit', SubmitType::class );

// Get request object
$request = mf_get_request();

// Handle request
$this->form->handleRequest( $request );

// Check if form is valid
if ( $this->form->isSubmitted() && $this->form->isValid() ) {

// Get data from the form
$data = $this->form->getData();

// Define filters
$filters = array(
'full_name' => FILTER_SANITIZE_STRING,
'email' => FILTER_SANITIZE_STRING | FILTER_SANITIZE_EMAIL,
'message' => FILTER_SANITIZE_STRING,
);

// Fields sanitization
$sanitized_data = filter_var_array( $data, $filters );

// Perform action with form data e.g. send an e-mail

// Redirect user with success parameter to prevent double submitting form
wp_safe_redirect( $this->get_redirect_url() );
}
}
}

/**
* Render contact form.
*
* This method is executed by contact shortcode.
*
* @return string
*/
public function render() {

$success = filter_input( INPUT_GET, 'success', FILTER_SANITIZE_NUMBER_INT );

if( '1' === $success ) {
return sprintf('

%s

', __('Thank you for submitting the form. We will contact you shortly.') );
}

$form_view = $this->form->createView();

$engine = mf_get_engine();

return $engine['form']->form( $form_view, array('attr' => array('novalidate' => 'novalidate') ) );

}

/**
* Method executed by constructor to define hooks and
* create and render contact form.
*
* @return void
*/
private function define_hooks() {

add_action( 'wp', array( $this, 'controller' ) );

add_shortcode( 'contact', array( $this, 'render' ) );
}

/**
* Build url for form redirect
*
* @return string
*/
private function get_redirect_url() {

$url = get_permalink();

$query = parse_url($url, PHP_URL_QUERY);

// Returns a string if the URL has parameters or NULL if not
if ($query) {
$url .= '&success=1';
} else {
$url .= '?success=1';
}

return $url;
}
}

// Initialize contact form
new ContactForm();

延伸相關外掛

文章
Filter
Apply Filters
Mastodon