|
2 | 2 |
|
3 | 3 | Here is a collection of code snippets that I use and have found helpful.
|
4 | 4 |
|
| 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) |
5 | 7 | * [Custom Page Titles](#custom-page-titles)
|
6 | 8 | * [Custom Page Titles with Yoast](#custom-page-titles-with-yoast)
|
7 | 9 | * [Customising the Custom Post Type Query](#customising-the-custom-post-type-query)
|
8 | 10 | * [Enabling Shortcodes within the Custom HTML Widget](#enabling-shortcodes-within-the-custom-html-widget)
|
9 | 11 | * [Send Post Comments to Custom Email Addresses](#send-post-comments-to-custom-email-addresses)
|
10 | 12 |
|
| 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 | + |
11 | 67 | ### Custom Page Titles
|
12 | 68 | Assuming you have added theme support for title-tag.
|
13 | 69 |
|
@@ -156,4 +212,4 @@ function ka_comment_moderation_recipients( $emails, $comment_id ) {
|
156 | 212 | add_filter( 'comment_moderation_recipients', 'ka_comment_moderation_recipients', 11, 2 );
|
157 | 213 | add_filter( 'comment_notification_recipients', 'ka_comment_moderation_recipients', 11, 2 );
|
158 | 214 |
|
159 |
| -``` |
| 215 | +``` |
0 commit comments