[WordPress] 外掛分享: Motiforms

前言介紹

  • 這款 WordPress 外掛「Motiforms」是 2017-11-09 上架。
  • 目前有 10 個安裝啟用數。
  • 上一次更新是 2017-11-10,距離現在已有 2732 天。超過一年沒更新,安裝要確認版本是否可用。以及後續維護問題!
  • 外掛最低要求 WordPress 3.8 以上版本才可以安裝。
  • 尚未有人給過這款外掛評分。
  • 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。

外掛協作開發者

kierzniak |

外掛標籤

form | forms | symfony | custom form | contact form |

內容簡介

警告

如果您不是開發人員,這個外掛不適合您。 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('

%s

', __('非常感謝您提交表單,我們將會盡快聯繫您。') ) ;
}

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

原文外掛簡介

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();

各版本下載點

  • 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
  • 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「Motiforms」來進行安裝。

(建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。


0.1.0 | trunk |

延伸相關外掛(你可能也想知道)

文章
Filter
Apply Filters
Mastodon