Skip to content

Commit c436154

Browse files
authored
Add Jetpack form filter examples
1 parent 544d232 commit c436154

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

README.md

+57-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,68 @@
22

33
Here is a collection of code snippets that I use and have found helpful.
44

5+
* [Add a pattern attribute to a Jetpack contact form field](#add-a-pattern-attribute-to-a-jetpack-contact-form-field)
6+
* [Add a max character limit to a Jetpack contact form field](#add-a-max-character-limit-to-a-jetpack-contact-form-field)
57
* [Custom Page Titles](#custom-page-titles)
68
* [Custom Page Titles with Yoast](#custom-page-titles-with-yoast)
79
* [Customising the Custom Post Type Query](#customising-the-custom-post-type-query)
810
* [Enabling Shortcodes within the Custom HTML Widget](#enabling-shortcodes-within-the-custom-html-widget)
911
* [Send Post Comments to Custom Email Addresses](#send-post-comments-to-custom-email-addresses)
1012

13+
14+
### Add a pattern attribute to a Jetpack contact form field
15+
16+
Restricting what URLs site visitors can enter into a Jetpack form URL field - in this case only YouTube URLs.
17+
18+
```php
19+
add_filter( 'grunion_contact_form_field_html', function( $rendered_field, $field_label, $id ) {
20+
if ( $field_label === 'Website' ) { // Update this value if your URL field has a different name.
21+
$rendered_field = preg_replace(
22+
'/pattern="([^"]*)"/',
23+
'pattern="^https:\/\/(?:(?:www\.)?youtube\.com|youtu\.be)\/.*$"',
24+
$rendered_field
25+
);
26+
// Update the custom validity message
27+
$rendered_field = preg_replace(
28+
'/setCustomValidity\\("[^&]*"\\)/',
29+
'setCustomValidity("Please enter a valid YouTube URL")',
30+
$rendered_field
31+
);
32+
}
33+
return $rendered_field;
34+
}, 10, 3 )
35+
```
36+
37+
### Add a max character limit to a Jetpack contact form field
38+
39+
Add a max character limit of 500 to the messages field, along with a span element explaining the limit.
40+
41+
```php
42+
add_filter( 'grunion_contact_form_field_html', function( $rendered_field, $field_label, $id ) {
43+
if ( $field_label === 'Message' && strpos( $rendered_field, '<textarea' ) !== false ) {
44+
$rendered_field = preg_replace(
45+
'/<textarea/',
46+
'<textarea maxlength="500"',
47+
$rendered_field,
48+
1
49+
);
50+
51+
$limit_text = sprintf(
52+
/* translators: %d: maximum number of characters allowed */
53+
esc_html__( 'Maximum %d characters allowed', 'your-text-domain' ),
54+
500
55+
);
56+
57+
$rendered_field = str_replace(
58+
'</div>',
59+
'<span id="message-limit-' . esc_attr($id) . '" class="message-limit-text" style="display: block; margin-top: 4px; font-size: 0.8em; color: #666;">' . $limit_text . '</span></div>',
60+
$rendered_field
61+
);
62+
}
63+
return $rendered_field;
64+
}, 10, 3 );
65+
```
66+
1167
### Custom Page Titles
1268
Assuming you have added theme support for title-tag.
1369

@@ -156,4 +212,4 @@ function ka_comment_moderation_recipients( $emails, $comment_id ) {
156212
add_filter( 'comment_moderation_recipients', 'ka_comment_moderation_recipients', 11, 2 );
157213
add_filter( 'comment_notification_recipients', 'ka_comment_moderation_recipients', 11, 2 );
158214

159-
```
215+
```

0 commit comments

Comments
 (0)