Snippets
These snippets can be placed in your theme's functions.php file or a code snippets plugin.
- Modify Marketing Consent
- Modify Terms Agreement
- Modify GDPR Consent
- Modify Confirmation Text
- Modify Footer: Privacy Policy
- Modify Footer: Terms
- Modify Footer: Unsubscribe
- Modify Footer: Don't want these emails?
- Modify "View this email in your browser."
- Hide Groundhogg Admin Widget
- Hide Contact Location fields
- Hide Contact Work Details fields
- Hide Contact Sources fields
- Hide Contact Compliance fields
- Capitalize first character for web form first name, last name
- Add a tag to a contact when they unsubscribe
- Add activity when note added
- Redirect visitors based on what option in a web form field is selected
- Remove the List-Unsubscribe header
- Add fake email address when importing Contacts with no email
- Automatically Delete a Groundhogg Contact When a WordPress User is Deleted
- Turn off IP Location
- Save a reply from Twilio into a custom field
- Add custom field to User Profile
- Register Custom Contact Tab
- Prevent emails from being sent from the staging site
Modify Marketing Consent
Default: I agree to receive marketing offers and updates from ( Business Name )
/**
* Modify the marketing consent field label.
*
* This function customizes the label for the marketing consent checkbox.
*
* @param string $label The default field label.
* @param string $field The field type.
* @return string The modified field label.
*/
function custom_marketing_consent_label( $label, $field ) {
if ( $field === 'marketing_consent' ) {
$label = __( 'Email me a weekly newsletter, exclusive products, and promo deals. You can unsubscribe at any time.', 'groundhogg' );
}
return $label;
}
add_filter( 'groundhogg/default_field_label', 'custom_marketing_consent_label', 10, 2 );
Modify Terms Agreement
Default: I agree to terms and conditions of ( Business Name )
/**
* Modify the terms and conditions field label to include a hyperlink.
*
* This function customizes the label for the "terms_agreement" checkbox field,
* inserting a clickable link to the terms and conditions page.
*
* @param string $label The default field label.
* @param string $field The field type.
* @return string The modified field label with a hyperlink.
*/
function custom_terms_and_conditions_label( $label, $field ) {
// Check if the field is the terms and conditions agreement checkbox.
if ( $field === 'terms_agreement' ) {
// Define the URL of the terms and conditions page.
$url = esc_url( 'https://example.com/terms/' );
// Modify the label to include a hyperlink to the terms and conditions.
$label = sprintf(
__( 'I agree to <a href="%s" target="_blank">terms and conditions</a> of %s.', 'groundhogg' ),
$url,
get_bloginfo()
);
}
return $label;
}
// Hook the custom function into the Groundhogg default field label filter.
add_filter( 'groundhogg/default_field_label', 'custom_terms_and_conditions_label', 10, 2 );
Modify GDPR Consent
Default: I agree to ( Business Name )'s storage and processing of my personal data.
/**
* Modify the GDPR consent field label.
*
* This function customizes the label for the "gdpr_consent" checkbox field,
* ensuring clarity and compliance with cookie policies.
*
* @param string $label The default field label.
* @param string $field The field type.
* @return string The modified GDPR consent field label.
*/
function custom_gdpr_consent_label( $label, $field ) {
// Check if the field is GDPR consent
if ( $field === 'gdpr_consent' ) {
$label = __(
'This website uses cookies to enhance your browsing experience. By checking the box, you consent to the use of all essential and non-essential cookies.',
'groundhogg'
);
}
return $label;
}
// Hook the custom function into the Groundhogg default field label filter.
add_filter( 'groundhogg/default_field_label', 'custom_gdpr_consent_label', 10, 2 );
Modify Confirmation Text
Default: Confirm your email.
/**
* Customize the confirmation text displayed after form submission.
*
* This function replaces the default confirmation text with a custom message.
*
* @param string $confirmation_text The default confirmation message.
* @return string The customized confirmation message.
*/
function custom_confirmation_text( $confirmation_text ) {
// Define the custom confirmation message
$custom_text = __( 'Your custom confirmation text goes here.', 'groundhogg' );
return $custom_text;
}
// Hook into the Groundhogg filter to modify the confirmation text.
add_filter( 'groundhogg/replacements/confirmation_text', 'custom_confirmation_text', 10 );
Modify Footer: Privacy Policy
Default: Privacy Policy
add_filter( 'gettext', 'gh_change_privacy_policy' );
/**
* Change "Privacy Policy" in footer
*
* @param $text string
* @return $text string
* * * * * * * * * * * * * * * * * * */
function gh_change_privacy_policy( $text ) {
$text = str_ireplace( "Privacy Policy", "Privacy Disclosure", $text );
return $text;
}
Modify Footer: Terms
Default: Terms
add_filter( 'gettext', 'gh_change_terms' );
/**
* Change "Terms" in footer
*
* @param $text string
* @return $text string
* * * * * * * * * * * * * * * * * * */
function gh_change_terms( $text ) {
$text = str_ireplace( "Terms", "Terms & Conditions", $text );
return $text;
}
Modify Footer: Unsubscribe
Default: Unsubscribe
add_filter( 'gettext', 'gh_change_unsubscribe' );
/**
* Change "Unsubscribe" in footer
*
* @param $text string
* @return $text string
* * * * * * * * * * * * * * * * * * */
function gh_change_unsubscribe( $text ) {
$text = str_ireplace( "Unsubscribe", "Custom link text", $text );
return $text;
}
Modify Footer: Don't want these emails?
Default: Unsubscribe
add_filter( 'gettext', 'gh_change_dont_want_these_emails' );
/**
* Change "Don't want these emails?" in footer
*
* @param $text string
* @return $text string
* * * * * * * * * * * * * * * * * * */
function gh_change_dont_want_these_emails( $text ) {
$text = str_ireplace( "Don't want these emails?", "Custom Message", $text );
return $text;
}
Modify "View this email in your browser."
// Change the "View this email in your browser." text add_filter( 'groundhogg/email_template/browser_view_text', 'Custom Message', 11 );
Hide Groundhogg Admin Widget
// Add custom CSS to WordPress dashboard
function remove_gh_admin_widget() {
echo '<style>
li#wp-admin-bar-groundhogg {
display: none;
}
</style>';
}
add_action('admin_head', 'remove_gh_admin_widget');
Hide Contact Location fields
function gh_hide_contact_location_info() {
echo '<style>
/* Hide H2 Location */
.top-left-square h2:nth-of-type(3) {
display: none;
}
.top-left-square .gh-rows-and-columns:nth-of-type(3) {
display: none;
}
</style>';
}
add_action('admin_head', 'gh_hide_contact_location_info');
Hide Contact Work Details fields
function gh_hide_contact_work_details_info() {
echo '<style>
/* Hide H2 Work Details */
.top-left-square h2:nth-of-type(2) {
display: none;
}
.top-left-square .gh-rows-and-columns:nth-of-type(2) {
display: none;
}
</style>';
}
add_action('admin_head', 'gh_hide_contact_work_details_info');
Hide Contact Sources fields
function gh_hide_contact_sources_info() {
echo '<style>
/* Hide H2 Sources */
.top-left-square h2:nth-of-type(4) {
display: none;
}
.top-left-square .gh-rows-and-columns:nth-of-type(4) {
display: none;
}
</style>';
}
add_action('admin_head', 'gh_hide_contact_sources_info');
Hide Contact Compliance fields
function gh_hide_contact_compliance_info() {
echo '<style>
/* Hide H2 Compliance */
.top-left-square h2:nth-of-type(5) {
display: none;
}
table.compliance-table {
display: none;
}
</style>';
}
add_action('admin_head', 'gh_hide_contact_compliance_info');
Capitalize first character for web form first name, last name
*Add classes( first_name_capitalize, last_name_capitalize ) to fields in the form.
function gh_first_last_capitalize() {
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery( '.first_name_capitalize' ).keyup(function() {
jQuery(this).val(jQuery(this).val().substr(0, 1).toUpperCase() + jQuery(this).val().substr(1).toLowerCase());
});
jQuery( '.last_name_capitalize' ).keyup(function() {
jQuery(this).val(jQuery(this).val().substr(0, 1).toUpperCase() + jQuery(this).val().substr(1).toLowerCase());
});
});
</script>
<?php
}
add_action( 'wp_head', 'gh_first_last_capitalize', 30 );
Add a tag to a contact when they unsubscribe
<?php
/**
* Add a tag to a contact when they unsubscribe.
*
* @param int $contact_id
* @param int $new_pref
* @param int $old_pref
* @param \Groundhogg\Contact $contact
*
* @return void
*/
function add_tag_when_unsubscribed( int $contact_id, int $new_pref, int $old_pref, \Groundhogg\Contact $contact ){
$contact->add_tag( 1234 ); // Change the ID of the tag to add
}
add_action( 'groundhogg/contact/preferences/unsubscribed', 'add_tag_when_unsubscribed', 10, 4 );
Add activity when note added
add_action( 'groundhogg/note/post_create', 'gh_track_activity_when_note_added', 10, 3 );
/**
* Adds activity whenever a note is added
*
* @param $id
* @param $data
* @param $note \Groundhogg\Classes\Note
*
* @return void
*/
function gh_track_activity_when_note_added( $id, $data, $note ){
// Ignore non ncontact notes
if ( $note->object_type !== 'contact' ){
return;
}
\Groundhogg\track_activity( $note->object_id, 'note_added' );
}
Redirect from Web Form, field option to URL
<?php
function maybe_do_form_redirect() {
// check for redirect flag in the URL
if ( absint( \Groundhogg\get_url_var( 'form_redirect' ) ) !== 1 ) {
return;
}
$contact = \Groundhogg\get_contactdata();
// if not a contact
if ( ! \Groundhogg\is_a_contact( $contact ) ) {
return;
}
$custom_field = $contact->get_meta( 'my_custom_field' );
switch ( $custom_field ) {
case 'foo':
$path = '/foo';
break;
case 'bar':
$path = '/bar';
break;
case 'baz':
$path = '/baz';
break;
}
wp_redirect( home_url( $path ) );
die;
}
add_action( 'template_redirect', 'maybe_do_form_redirect' );
Remove the List-Unsubscribe header
add_filter( 'groundhogg/list_unsubscribe_header', '__return_empty_string' );
Add fake email address when importing Contacts with no email
add_action( 'groundhogg/generate_contact_with_map/before', function ( &$fields, &$map ) {
if ( ! \Groundhogg\isset_not_empty( $fields, 'email' ) ){
// Add a fake email address to the response
$fields['email'] = uniqid( 'fake-email-' ) . '@fake.com';
// Add the email field to the map
$map['email'] = 'email';
}
}, 10, 2 ); // Add priority (10) and argument count (2)
Automatically Delete a Groundhogg Contact When a WordPress User is Deleted
add_action( 'delete_user', function ( $user_id ) {
// Get the related contact record
$contact = \Groundhogg\get_contactdata( $user_id );
// Ensure $contact is a valid object before calling exists()
if ( $contact && is_object( $contact ) && method_exists( $contact, 'exists' ) && $contact->exists() ) {
// Delete the contact
$contact->delete();
// Log the deletion for debugging (optional)
error_log( "Deleted Groundhogg contact for user ID: $user_id" );
} else {
// Log if the contact was not found
error_log( "No Groundhogg contact found for user ID: $user_id" );
}
});
Turn off IP location
<?php /** * Disable IP-based location extrapolation in Groundhogg. * * Filter prevents Groundhogg from using IP addresses to determine a user's location. * Useful for improving privacy and compliance with data regulations. * * @since 1.0.0 */ add_filter( 'groundhogg/should_extrapolate_location', '__return_false' );
Save a reply from Twilio into a custom field
<?php
/**
* Save a reply from twilio into a custom field
*
* @param \Groundhogg\Classes\Activity $activity the activity with sms info
* @param \Groundhogg\Contact $contact the contact
*
* @return void
*/
function save_reply_to_custom_field( \Groundhogg\Classes\Activity $activity, \Groundhogg\Contact $contact ){<br>
// Sanitize the SMS body
$body = sanitize_text_field( $activity->body );
// Allow developers to modify the meta key and value
$meta_key = apply_filters( 'groundhogg/twilio/reply_meta_key', 'sms_reply' );
$meta_value = apply_filters( 'groundhogg/twilio/reply_meta_value', $body );
// Update meta and check for errors
$updated = $contact->update_meta( $meta_key, $meta_value );
if ( ! $updated ) {
error_log( sprintf( 'Failed to save SMS reply for contact ID: %d', $contact->get_id() ) );
}
}<br>
add_action( 'groundhogg/track_activity/twilio_reply', 'save_reply_to_custom_field', 10, 2 );
Add custom field to User profile
// Add Driver's License field to user profile
add_action('show_user_profile', 'add_drivers_license_field');
add_action('edit_user_profile', 'add_drivers_license_field');
function add_drivers_license_field($user) {
// Only show to users who can edit users
if (!current_user_can('edit_users') && !current_user_can('edit_user', $user->ID)) {
return;
}
?>
<h3><?php _e('Additional Information', 'your-theme-text-domain'); ?></h3>
<table class="form-table">
<tr>
<th><label for="drivers_license"><?php _e('Driver\'s License Number', 'your-theme-text-domain'); ?></label></th>
<td>
<input type="text"
name="drivers_license"
id="drivers_license"
value="<?php echo esc_attr(get_user_meta($user->ID, 'drivers_license', true)); ?>"
class="regular-text" />
<p class="description"><?php _e('Enter the user\'s driver\'s license number.', 'your-theme-text-domain'); ?></p>
</td>
</tr>
</table>
<?php
}
// Save Driver's License field
add_action('personal_options_update', 'save_drivers_license_field');
add_action('edit_user_profile_update', 'save_drivers_license_field');
function save_drivers_license_field($user_id) {
// Check if user has permission to edit
if (!current_user_can('edit_user', $user_id)) {
return false;
}
// Verify nonce for security (optional but recommended)
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'update-user_' . $user_id)) {
return;
}
// Sanitize and update the field
if (isset($_POST['drivers_license'])) {
$drivers_license = sanitize_text_field($_POST['drivers_license']);
update_user_meta($user_id, 'drivers_license', $drivers_license);
}
}
The use {user.drivers_license} replacement code in, email funnels, etc.
Register Custom Contact Tab
Register a custom tab in the contact profile view
<?php
class Custom_Contact_Tab {
public $tab_id = 'my_custom_tab';
public $tab_name = 'My Custom Tab';
public function __construct() {
add_filter( 'groundhogg/admin/contact/record/tabs', [ $this, 'register' ] );
add_action( 'groundhogg/admin/contact/record/tab/' . $this->tab_id, [ $this, 'callback' ] );
}
/**
* @param $tabs
*
* @return mixed
*/
public function register( $tabs ) {
$tabs[$this->tab_id] = $this->tab_name;
return $tabs;
}
/**
* Output any HTML for the custom tab
*
* @param \Groundhogg\Contact $contact
*
* @return void
*/
public function callback( \Groundhogg\Contact $contact ) {
// todo your tab content
}
}
// Instantiate the custom tab
new Custom_Contact_Tab();
Prevent emails from being sent from the staging site
add_filter('wp_mail', function ($args) {
if (isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] === 'staging.mysite.com') {
return false; // Block all emails on staging
}
return $args;
});