Loading...
Skip to main content

Conditionally Show Form on Single Listing – Business Directory Plugin

http://businessdirectoryplugin.com/docs/customization-guide/ says you cannot override the contact form template.

however, within the plugin itself, there is a filter that does allow this functionality: wpbdp_show_contact_form

so, we only want to show this form based on an if statement.

The Business Directory Plugin also has options/settings that allows you to add a form (and form fields) to each listing, backend and frontend views. Some are added as post_meta and others are simply fields to be emailed.

What we are attempting to do is confirm if a post_meta field, added by the plugin, has value, and if so, then show the frontend view of the contact form. The contact form itself is generated by the plugin and has it’s own ID. The parameter for the above filter does allow that same ID to be passed through it, but it was not capturing, and come to find out that the form ID and the page you are displaying this form on have the same ID, so we need to capture that with WordPress standard get_the_ID(); which gets the current page/post ID.

The next thing we need is the Business Directory option (‘show-contact-form’). With a handy little helper of _get_option we can grab the current setting from the set options.

And the last thing, which is our conditional statement, is getting the $field object value from the ID. If this field has a value, show the form, if not, keep it hidden.

Here is the final piece of code:

add_filter('wpbdp_show_contact_form', 'rf_yes_no');
function rf_yes_no( $listing_id ){
	$listingid = get_the_ID();
	$yes = wpbdp_get_option( 'show-contact-form' ); // 1 = show
	$field = wpbdp_get_form_field( 8 );
	$fieldmail = $field->value( $listingid ); 
	if (!empty($fieldmail)) {
		$yes = 1;
	} else {
		$yes = 0;
	}
	return $yes;
}

As you can see thee are actually three items you need, and then your conditional element (does the field have a value), then change the option accordingly.

Here is the listing view with no email value:

And now you get this with your new code (placed in a plugin or theme functions.php):

Let me know if you find this helpful and if you see any modifications that may be necessary.

No Comments yet!

Your Email address will not be published.


Shopping cart0
There are no products in the cart!
Continue shopping