Skip to content

Commit 00fa0cd

Browse files
author
Antti Kuosmanen
authored
Merge pull request #5 from k1sul1/master
Allow enqueueing the script by registering it outside the if-clause + readme mentions
2 parents 38e97bc + a5d04e1 commit 00fa0cd

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

classes/class-cpt-wplf-form.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,11 @@ function wplf_form( $id , $content = '', $xclass = '' ) {
445445
function maybe_enqueue_frontend_script() {
446446
global $post;
447447

448-
// only enqueue script if current post contains a form in it
448+
// register the script, but only enqueue it if the current post contains a form in it
449+
wp_register_script( 'wplf-form-js', plugins_url( 'assets/scripts/wplf-form.js', dirname(__FILE__) ), array( 'jquery' ) );
450+
449451
if( is_a( $post, 'WP_Post' ) && ( has_shortcode( $post->post_content, 'libre-form') || $post->post_type === 'wplf-form') ) {
450-
wp_enqueue_script( 'wplf-form-js', plugins_url( 'assets/scripts/wplf-form.js', dirname(__FILE__) ), array( 'jquery' ) );
452+
wp_enqueue_script( 'wplf-form-js' );
451453
wp_localize_script( 'wplf-form-js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
452454
}
453455
}
@@ -480,4 +482,3 @@ function use_shortcode_for_preview( $content ) {
480482
}
481483

482484
endif;
483-

readme.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ function my_email_thankyou( $return ) {
7979
}
8080
```
8181

82+
### Use shortcodes outside post content
83+
84+
By default, scripts are only loaded when the shortcode is within the content.
85+
If you use shortcodes outside the content, ex. custom fields or by `do_shortcode`, you need to manually enqueue the scripts for the submit to work.
86+
87+
```php
88+
wp_enqueue_script('wplf-form-js');
89+
wp_localize_script( 'wplf-form-js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
90+
```
91+
92+
### Add own css classes to form output
93+
94+
You can use the attribute xclass inside the shortcode to set own extra css classes.
95+
96+
```
97+
[libre-form id="1" xclass="extra"]
98+
```
99+
100+
82101
### Filter: wplf_validate_submission
83102

84103
Used to add validation to your forms

0 commit comments

Comments
 (0)