-
Notifications
You must be signed in to change notification settings - Fork 3
/
readme.txt
194 lines (146 loc) · 4.68 KB
/
readme.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
=== Motiforms ===
Contributors: kierzniak
Tags: form, forms, custom form, contact form, symfony, symfony form
Requires at least: 3.8
Tested up to: 4.8.3
Stable tag: 0.1.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Motiforms is a WordPress plugin provided for creating forms programmatically using Symfony framework.
== Description ==
= 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('<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();
`
== Installation ==
1. Visit Plugins > Add New
2. Search for “Motiforms”
3. Install and activate “Motiforms"
4. Go to [get started](https://github.com/motivast/motiforms#user-content-get-started) section to see a simple example.
or
1. Download plugin from wordpres.org repository or [release section](https://github.com/motivast/motiforms/releases/latest).
2. Upload the motiforms directory to your /wp-content/plugins/ directory
3. Activate the plugin through the"‘Plugins" menu in WordPress
4. Go to [get started](https://github.com/motivast/motiforms#user-content-get-started) section to see a simple example.
== Frequently Asked Questions ==
= Where I can find documentation? =
Documentation for Motiforms can be found on github [wiki pages](https://github.com/motivast/motiforms/wiki).
== Changelog ==
= 0.1.0 =
* Motiforms