Replacing the Divi Pricing Table Button

This post may contain referral links which may earn a commission for this site

The Divi Theme's pricing table module allows you to add a button at the end of each pricing table item. The "button" is a really just a link, with text and a URL, styled to look like a button. So what if you need to replace it with something more complex, such as a form or button generated by a shortcode?

Tip: To use shortcodes within the pricing table fields (e.g. button text and link), try my Divi Shortcode Enabler plugin.

Filtering the Pricing Button HTML in PHP

One option is to modify the button HTML through PHP. Divi doesn't give us a way to do this directly. However, it does give us a way to modify the HTML for an entire pricing table item, using a WordPress "filter" called "et_pb_pricing_table_shortcode_output". We'll use this to first create a new filter, called "db_pricing_table_shortcode_output_button" which modifies just the button HTML:

// === Add "db_pricing_table_shortcode_output_button" filter ===

add_filter('et_pb_pricing_table_shortcode_output', 'dbcf_add_pricing_table_button_filter');

function dbcf_add_pricing_table_button_filter($html) {
	if (!is_admin() && !isset($_GET['et_fb'])) {
		$button_wrapper_regex = preg_quote('<div class="et_pb_button_wrapper">', '/').'.*?'.preg_quote('</div>', '/');
		$html = preg_replace_callback("/$button_wrapper_regex/s", 'dbcf_add_pricing_table_button_filter_callback', $html);
	}
	return $html;
}

function dbcf_add_pricing_table_button_filter_callback($match) {
	$button = isset($match[0])?$match[0]:'';
	return apply_filters('db_pricing_table_shortcode_output_button', $button);
}
This filter can now be used to modify the button HTML, and works in the same way as any other WordPress filter.

Example: Simple Membership Payment Button

The Simple Membership Payment plugin provides a way to create PayPal and Stripe payment buttons. These can be added to a page using the "swpm_payment_button" shortcode (described here). The output of the shortcode is a form – too complex to be implemented using the existing pricing table button. But we can use our new filter to replace the pricing table button with the output of the shortcode. like so:

add_filter('db_pricing_table_shortcode_output_button', 'dbcf_replace_pricing_table_button');

function dbcf_replace_pricing_table_button($button) {
	$swpm_button_regex = preg_quote('[swpm_payment_button', '/').'(.*?)'.preg_quote(']', '/');
	if (preg_match('/'.$swpm_button_regex.'/s', $button, $match)) {
		$swpm_button_shortcode = '[swpm_payment_button'.html_entity_decode($match[1]).']';	
		$swpm_button = do_shortcode($swpm_button_shortcode);
		$swpm_button = str_replace('class="swpm-buy-now-button-submit"', 'class="swpm-buy-now-button-submit et_pb_button et_pb_pricing_table_button"', $swpm_button);
		return $swpm_button;
	}
	return $button;
}

// Stop Divi Shortcode Enabler from processing the pricing table button URL if active
add_filter('dbdsp_fields_to_process', 'dbcf_stop_processing_of_shortcode_in_pricing_table');

function dbcf_stop_processing_of_shortcode_in_pricing_table($modules) {
	if (isset($modules['et_pb_pricing_table']['button_url'])) { 
		unset($modules['et_pb_pricing_table']['button_url']);
	}
	return $modules;
}
Now to use it:

  • Paste your payment button shortcode into the "Content > Text > Button" field of the pricing table – i.e. as the name of the button
  • Set the pricing table's "Content > Link > Button Link URL" field to "#" (so Divi actually displays the button).

The pricing table button should be replaced with the Simple Membership Plugin payment button. The code above also adds the pricing table button CSS classes to the payment button, so that it picks up (most of) the styles from the Divi pricing table button settings. A notable exception is that the button icons won't work (since they aren't supported on input elements such as used by the payment button). You may like to disable the button icon in the pricing table settings to prevent a blank space showing on the button on hover.

Hundreds of new features for Divi
in one easy-to-use plugin

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *