motivast / motiforms
Motiforms 是一个用于程序化创建表单的插件。
0.1.0
2017-11-10 14:24 UTC
Requires
- php: >=5.6.0
- symfony/config: 3.3.*
- symfony/form: 3.3.*
- symfony/framework-bundle: 3.3.*
- symfony/templating: 3.3.*
- symfony/translation: 3.3.*
- symfony/validator: 3.3.*
Requires (Dev)
- php: >=5.6.0
- composer/installers: 1.2.0
- friendsofphp/php-cs-fixer: 1.11.4
- johnpbloch/wordpress: ~4
- pdepend/pdepend: 2.2.4
- phing/phing: 2.16.0
- phploc/phploc: 4.0.0
- phpmd/phpmd: 2.4.2
- phpunit/phpunit: 5.7.21
- sebastian/phpcpd: 3.0.0
- squizlabs/php_codesniffer: 2.8.1
- symfony/config: 3.3.*
- symfony/css-selector: 3.3.*
- symfony/dom-crawler: 3.3.*
- symfony/form: 3.3.*
- symfony/framework-bundle: 3.3.*
- symfony/templating: 3.3.*
- symfony/translation: 3.3.*
- symfony/validator: 3.3.*
- wordpress/wordpress-dev: 4.8
- wp-cli/wp-cli: 1.2.1
- wp-coding-standards/wpcs: 0.11.0
This package is auto-updated.
Last update: 2024-09-10 01:09:58 UTC
README
Motiforms 是一个使用 Symfony 框架程序化创建表单的 WordPress 插件。如果您不需要表单构建器而想创建高级表单,请查看 Motiforms。
前往 入门 部分,查看一个简单示例。
警告
如果您不是开发者,则此插件不适合您。Motiforms 不提供任何 WordPress 管理界面来创建表单。
特性
- 处理表单逻辑
- 字段序列化
- 字段验证
- 内置 html 渲染助手
- 灵活性
- 基于先进的 Symfony 框架
安装
- 从 wordpres.org 存储库下载插件或 发布部分。
- 将 motiforms 目录上传到您的 /wp-content/plugins/ 目录
- 通过 WordPress 中的“插件”菜单激活插件
开始使用
要创建简单的联系表单,请将以下代码粘贴到您的 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 { /** * 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('<h2>%s</h2>', __('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();
文档
Motiforms 的文档可以在 github 上的 wiki 页面 上找到。
贡献
在提交拉取请求之前,请务必阅读 贡献指南。
感谢所有为 Motiforms 做出贡献的人!
许可证
该项目受 GNU GPLv2 (或更高版本) 许可。
版权 (c) 2017-至今,Motivast