Title: Validate custom shortcode
Last modified: November 2, 2016

---

# Validate custom shortcode

 *  [ProxxiM](https://wordpress.org/support/users/proxxim/)
 * (@proxxim)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/validate-custom-shortcode/)
 * Hi guys,
 * I’ve created a custom shortcode that retrieves the title of a certain custom 
   post type. These titles are placed in a select box.
 * I want to make sure that the user has selected one of these titles when filling
   in the form, so I guess some validation is needed. The [official documentation about this](http://contactform7.com/2015/03/28/custom-validation/)
   doesn’t seem to be sufficient for me and I can’t find any useful topics that 
   help me out.
 * Here I create the shortcode:
 *     ```
       function upcoming_events($tag){
       	$tag = new WPCF7_Shortcode( $tag );
   
       	wp_reset_query();
   
       	$output = "<select id='locatie' name='locatie' class='wpcf7-form-control wpcf7-select form-control' onchange='document.getElementById(\"locatie\").value=this.value;'><option disabled selected value> -- selecteer een locatie -- </option>";
   
       	global $post;
   
       	$locaties_args = array(
       		'post_type' => 'speeldata',
       		'posts_per_page' => -1,
       		'orderby' => 'name',
       		'order' => 'ASC',
       		'meta_query' => array(
       			array(
       				'key' => 'date',
       				'value' => date("Y-m-d"),
       				'compare' => '>=',
       				'type' => 'DATE'
       			),
       		),
       	);
   
       	$query = new WP_Query( $locaties_args );
   
       	while( $query->have_posts() ) : $query->the_post();
       		//var_dump($post);
       		$atts = fw_get_db_post_option(get_the_id());
   
       		setlocale(LC_ALL, 'nl_NL');
       		$local_date = trim(strftime("%e %B %Y", strtotime($atts['datum'])));
   
       		//var_dump($local_date);
   
       		//var_dump($atts);
       		$output .= "<option value='". $post->post_name ."'>". get_the_title() ." (". $local_date .")</option>";
       	endwhile;
   
       	$output .= "</select>";
   
       	return $output;
       }
   
       function custom_add_shortcode() {
       	wpcf7_add_shortcode('upcoming-events', 'upcoming_events', true);
       	wpcf7_add_shortcode('upcoming-events*', 'upcoming_events', true);
       }
   
       add_action( 'wpcf7_init', 'custom_add_shortcode' );
       ```
   
 * This all works fine, the value is passed to the mail after the form gets submitted.
   The only problem is that I want the user to actively make a selection, so I need
   a default empty value and validation.
 * I got this so far, I know this is nowhere near the solution but when adding more
   code I the debug console throws 500 internal server errors at me.
 *     ```
       function upcoming_events_validation( $result, $tag) {
           $tag = new WPCF7_Shortcode( $tag );
   
           $result->invalidate( $tag, "Selecteer een locatie" );
   
           return $result;
       }
       add_filter( 'wpcf7_validate_upcoming_events*', 'upcoming_events_validation');
       ```
   
    -  This topic was modified 9 years, 5 months ago by [ProxxiM](https://wordpress.org/support/users/proxxim/).
      Reason: some explanation

Viewing 1 replies (of 1 total)

 *  Thread Starter [ProxxiM](https://wordpress.org/support/users/proxxim/)
 * (@proxxim)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/validate-custom-shortcode/#post-8390987)
 * A little update, I’ve got the verification working with this:
 *     ```
       function upcoming_events_validation( $result, $tag) {
           //$tag = new WPCF7_Shortcode($tag);
           $name = $tag['name'];
   
       	if (empty($_POST[$name])) {
           	$result->invalidate( $tag, "Selecteer een locatie");
       	} else {
           	$result['valid'] = true;
       	}
   
           return $result;
       }
       add_filter( 'wpcf7_validate_upcoming_events*', 'upcoming_events_validation', 20, 2);
       ```
   
 * I’m not sure what’s wrong with “new WPCF7_Shortcode($tag);”, but when it’s active
   I get the server error again.
 * The only thing missing is an explicit warning message for the input field. Can
   someone give me pointers on how to achieve that?

Viewing 1 replies (of 1 total)

The topic ‘Validate custom shortcode’ is closed to new replies.

 * ![](https://ps.w.org/contact-form-7/assets/icon.svg?rev=2339255)
 * [Contact Form 7](https://wordpress.org/plugins/contact-form-7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-7/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-7/reviews/)

## Tags

 * [custom shortcode](https://wordpress.org/support/topic-tag/custom-shortcode/)
 * [validation](https://wordpress.org/support/topic-tag/validation/)

 * 1 reply
 * 1 participant
 * Last reply from: [ProxxiM](https://wordpress.org/support/users/proxxim/)
 * Last activity: [9 years, 5 months ago](https://wordpress.org/support/topic/validate-custom-shortcode/#post-8390987)
 * Status: not resolved