Last active
November 29, 2019 09:56
-
-
Save Jehu/5d7636cf54b1a6913a91549f2a4505a3 to your computer and use it in GitHub Desktop.
Use Groundhogg contact fields inside WPForms as smart tags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WPForms GH Contact Smart Tag | |
* Plugin URI: | |
* Description: Use Groundhogg contact fields inside WPForms with Smart Tags. Se here for more informations about WPFOrms Smart Tags: https://wpforms.com/docs/how-to-use-smart-tags-in-wpforms/ | |
* Version: 1.0 | |
* Author: Marco Michely | |
* Author URI: https://www.michely-web-engineering.de | |
*/ | |
defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); | |
/** | |
* Register the Smart Tag so it will be available to select in the form builder. | |
* | |
* @link https://wpforms.com/developers/how-to-create-a-custom-smart-tag/ | |
* | |
* @param array $tags | |
* @return array | |
*/ | |
function mwewpfghst_register_smarttag( $tags ) { | |
if(is_plugin_active( 'groundhogg/groundhogg.php' )) { | |
// Key is the tag, item is the tag name. | |
$tags['ghcontact_first'] = "GH Contact First Name"; | |
$tags['ghcontact_last'] = "GH Contact Last Name"; | |
$tags['ghcontact_full_name'] = "GH Contact Full Name"; | |
$tags['ghcontact_job_title'] = "GH Contact Job Title"; | |
$tags['ghcontact_email'] = "GH Contact EMail"; | |
$tags['ghcontact_phone'] = "GH Contact Phone"; | |
$tags['ghcontact_phone_ext'] = "GH Contact Phone number extension"; | |
$tags['ghcontact_address'] = "GH Contact Address"; | |
$tags['ghcontact_company_name'] = "GH Contact Company Name"; | |
$tags['ghcontact_company_address'] = "GH Contact Company Address"; | |
$tags['ghcontact_username'] = "GH Contact User Name"; | |
// $tags['ghcontact_'] = "GH Contact "; | |
} | |
return $tags; | |
} | |
add_filter( 'wpforms_smart_tags', 'mwewpfghst_register_smarttag' ); | |
/** | |
* Process the Smart Tag. | |
* | |
* @link https://wpforms.com/developers/how-to-create-a-custom-smart-tag/ | |
* | |
* @param string $content | |
* @param string $tag | |
* @return string | |
*/ | |
function mwewpfghst_process_smarttag( $content, $tag ) { | |
// Only run if it is our desired tag. | |
if ( strpos($tag, "ghcontact_") === 0 ) { | |
$gh_field = str_replace("ghcontact_", "", $tag); | |
// Replace the tag with Groundhogg contact information. | |
$sc_processed = do_shortcode('[gh_is_contact][gh_replacements]{'.$gh_field.'}[/gh_replacements][/gh_is_contact]'); | |
/* $sc_processed = do_shortcode("[gh_is_contact][gh_contact | |
field='".$gh_field."'][/gh_is_contact]"); */ | |
$replacement = ($sc_processed !== $gh_field) ? $sc_processed : NULL; | |
$content = str_replace( '{ghcontact_' . $gh_field . '}', $replacement, $content ); | |
} | |
return $content; | |
} | |
add_filter( 'wpforms_smart_tag_process', 'mwewpfghst_process_smarttag', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment