Validation filter for custom CF7 shortcode
-
I’ve added a custom post dropdown. It shows up in my website. But the validation for this dropdown doesn’t work
Here is the code:
function custom_add_careerdropdown() { wpcf7_add_shortcode( 'careerdropdown', 'custom_careerdropdown_shortcode_handler', true ); wpcf7_add_shortcode( 'careerdropdown*', 'custom_careerdropdown_shortcode_handler', true ); } function custom_careerdropdown_shortcode_handler( $tag ) { $args = array( 'post_type' => 'career', 'posts_per_page' => '-1', 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC', ); $careerposts = new WP_Query( $args ); if( $careerposts->have_posts() ): $selectoutput = "<select name='cursus' id='cursus' onchange='document.getElementById(\"cursus\").value=this.value;'><option></option>"; while( $careerposts->have_posts() ) : $careerposts->the_post(); $title = get_the_title(); $selectoutput .= "<option value='$title'> $title </option>"; endwhile; $selectoutput .= "</select>"; endif; return $selectoutput; } add_filter( 'wpcf7_validate_careerdropdown', 'wpcf7_careerdropdown_validation_filter', 10, 2 ); add_filter( 'wpcf7_validate_careerdropdown*', 'wpcf7_careerdropdown_validation_filter', 10, 2 ); function wpcf7_careerdropdown_validation_filter( $result, $tag ) { $type = $tag['type']; $name = $tag['name']; $_POST[$name] = trim( strtr( (string) $_POST[$name], "\n", " " ) ); if ( 'careerdropdown*' == $type || 'careerdropdown' == $type) { if ( '' == $_POST[$name] ) { $result['valid'] = false; $result['reason'][$name] = wpcf7_get_message( 'invalid_required' ); } } return $result; }
Here is the shortcode in the form editor:
<label>Select job<span class=”required”>*</span>
[careerdropdown* selectedjob]</label>
- The topic ‘Validation filter for custom CF7 shortcode’ is closed to new replies.