[Documentation](https://groundhogg.io/documentation/) » [Code Samples](https://groundhogg.io/documentation/#doc-code-samples)

# Snippets

Published Thursday, May 8, 2025 · Last updated Wednesday, September 24, 2025

- ## [API Trigger for when a contact owner is changed](https://groundhogg.io/codesample/api-trigger-for-when-a-contact-owner-is-changed/)

  Call a custom API trigger whenever a contact owner is changed.

  ```

  <?php
  
  add_action('groundhogg/contact/owner_changed', 'do_trigger_when_owner_changed', 10, 3 );
  
  /**
   * Call an custom Trigger whenever the contact owner is changed
   *
   * @param  WP_User  $new_owner
   * @param  \Groundhogg\Contact  $contact
   * @param  WP_User  $old_owner
   *
   * @return void
   */
  function do_trigger_when_owner_changed( WP_User $new_owner, \Groundhogg\Contact $contact, WP_User $old_owner  ) {
  	\Groundhogg\do_api_trigger( 'contact_owner_changed', $contact );
  }
  ```
- ## [Change “View this email in your browser.”](https://groundhogg.io/codesample/change-view-this-email-in-your-browser/)

  ```

  <?php
  
  // Change the "View this email in your browser." text
  add_filter( 'groundhogg/email_template/browser_view_text', 'Custom Message', 11 );
  ```
- ## [Change the “Don’t want these emails?”](https://groundhogg.io/codesample/change-the-dont-want-these-emails/)

  ```

  <?php
  /**
    * 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;
  }
  add_filter( 'gettext', 'gh_change_dont_want_these_emails' );
  ```
- ## [Change the confirmation link text](https://groundhogg.io/codesample/change-the-confirmation-link-text/)

  Changes the text of the confirmation link when using the {confirmation_link} replacement code.

  ```

  <?php
  
  /**
   * 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 );
  ```
- ## [Change the data processing consent checkbox text](https://groundhogg.io/codesample/change-the-data-processing-consent-checkbox-text/)

  Changes the text in Groundhogg forms for the data processing consent (GDPR) checkbox field

  ```

  <?php
  
  /**
   * 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 );
  ```
- ## [Change the marketing consent checkbox text](https://groundhogg.io/codesample/change-the-marketing-consent-checkbox-text/)

  Changes the marketing consent checkbox text in Groundhogg forms with your own custom text.

  ```

  <?php
  
  /**
   * 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 );
  ```
- ## [Change the privacy policy link text in emails](https://groundhogg.io/codesample/change-the-privacy-policy-link-text-in-emails/)

  Changes the text of the privacy policy link in the legacy email footer. Does not apply when using the footer block.

  ```

  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;
  }
  ```
- ## [Change the terms agreement checkbox text](https://groundhogg.io/codesample/change-the-terms-agreement-checkbox-text/)

  Changes the terms agreement checkbox text in Groundhogg forms.

  ```

  <?php
  
  /**
   * 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 );
  ```
- ## [Change the terms link text in emails](https://groundhogg.io/codesample/change-the-terms-link-text-in-emails/)

  Changes the text of the terms link in the legacy email footer. Does not apply when using the footer block.

  ```

  <?php
  /**
   * Modifies the "Terms" text to "Terms & Conditions" in the footer context.
   *
   * @param string $translated_text The translated text.
   * @param string $text                    The original text before translation.
   * @param string $domain              The text domain.
   * @return string                              The modified text.
   */
  function gh_change_footer_terms( string $translated_text, string $text, string $domain ): string {
      // Only apply the replacement in the footer context and for a specific text domain (if applicable).
      if ( 'footer' === $domain || in_array( $domain, [ 'your-theme-text-domain', 'default' ], true ) ) {
          if ( 'Terms' === $text ) {
              return 'Terms & Conditions';
          }
      }
  
      return $translated_text;
  }
  add_filter( 'gettext', 'gh_change_footer_terms', 20, 3 );
  ```
- ## [Change the unsubscribe link text in emails](https://groundhogg.io/codesample/change-the-unsubscribe-link-text-in-emails/)

  Changes the text of the unsubscribe link.

  ```

  /**
    * 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;
  }
  add_filter( 'gettext', 'gh_change_unsubscribe' );
  ```
- ## [Get an array of tags using the PHP API](https://groundhogg.io/codesample/get-an-array-of-tags-using-the-php-api/)

  Use the Query API to fetch a list of tags that match the provided slugs.

  ```

  <?php 
  
  $tagQuery = new \Groundhogg\DB\Query\Table_Query( 'tags' );
  $tagQuery->where()->in( 'tag_slug', [
  	'tag1',
  	'tag2',
  	'tag3',
  ] );
  
  $tags = $tagQuery->get_objects( \Groundhogg\Tag::class );
  ```
- ## [Hide Contact Location fields](https://groundhogg.io/codesample/hide-contact-location-fields/)

  ```

  <?php
  function gh_hide_contact_location_info() {
      echo '
          /* 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;
          }
      ';
  }
  add_action('admin_head', 'gh_hide_contact_location_info');
  ```
- ## [Hide Contact Work Details fields](https://groundhogg.io/codesample/hide-contact-work-details-fields/)

  ```

  <?php
  function gh_hide_contact_work_details_info() {
      echo '
          /* 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;
          }
      ';
  }
  add_action('admin_head', 'gh_hide_contact_work_details_info');
  ```
- ## [Hide Groundhogg Admin Widget](https://groundhogg.io/codesample/hide-groundhogg-admin-widget/)

  Hide the Groundhogg Admin Widget with CSS

  ```

  <?php
  // Add custom CSS to WordPress dashboard
  function remove_gh_admin_widget() {
      echo '
  		li#wp-admin-bar-groundhogg {
      		display: none;
  		}
  	';
  }
  add_action('admin_head', 'remove_gh_admin_widget');
  ```
- ## [Plugin info custom replacement code](https://groundhogg.io/codesample/plugin-info-custom-replacement-code/)

  Adds a custom replacement code that allows developers to showcase stats from their WordPress.org plugin listing.

  ```

  <?php
  
  add_action( 'groundhogg/replacements/init', function ( \Groundhogg\Replacements $replacements ){
  
  	// usage would look like
  	// `{plugin_info.slug="groundhogg" field="downloaded"}`
  	$replacements->add( 'plugin_info', function ( $args ){
  
  		$args = shortcode_parse_atts( $args );
  		$args = wp_parse_args( $args, [
  			'slug' => '',
  			'field' => 'active_installs'
  		] );
  
  		if ( empty( $args['slug'] ) ) {
  			return 'specific a slug';
  		}
  
  		if ( ! function_exists( 'plugins_api' ) ) {
  			require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
  		}
  
  		$root_field = explode( '.', $args['field'] )[0];
  
  		$result = plugins_api( 'plugin_information', [
  			'slug' => $args['slug'],
  			'fields' => [
  				$root_field => true // make sure the root field is in the request
  			]
  		] );
  
  		if ( is_wp_error( $result ) ){
  			return 'Failed to fetch plugin info.';
  		}
  
  		// this allows us to use field like "ratings.0.whatever"
  		$return = get_by_dotn( $result, $args['field'] );
  
  		if ( in_array( $args['field'], [ 'active_installs', 'downloaded', 'num_ratings' ] )){
  			$return = number_format( $return );
  		}
  
  		return $return;
  
  	}, 'Fetch plugin info from WordPress.org', 'Plugin info', 'site', 'slug="groundhogg" field="active_installs"' );
  
  } );
  
  if ( ! function_exists( 'get_by_dotn' ) ){
  
  	/**
  	 * Get a value from an object or array using dot notation instead of direct key access
  	 *
  	 * @param $data array|object
  	 * @param $path string
  	 * @param $default mixed
  	 *
  	 * @return mixed|null
  	 */
  	function get_by_dotn($data, $path, $default = null) {
  		if ($data === null || $path === '') {
  			return $default;
  		}
  
  		$keys = explode('.', $path);
  
  		foreach ($keys as $key) {
  			// convert purely numeric keys to int for array index access
  			if (ctype_digit($key)) {
  				$key = (int) $key;
  			}
  
  			if (is_array($data) && array_key_exists($key, $data)) {
  				$data = $data[$key];
  			} elseif (is_object($data) && (isset($data->$key) || property_exists($data, $key))) {
  				$data = $data->$key;
  			} else {
  				return $default;
  			}
  		}
  
  		return $data;
  	}
  
  }
  ```
- ## [Register a custom replacement code and group](https://groundhogg.io/codesample/register-a-custom-replacement-code-and-group/)

  Sample on registering your own custom replacement groups and codes.

  ```

  <?php
  
  add_action( 'groundhogg/replacements/init', 'add_my_custom_replacements');
  
  /**
   * Register custom replacement groups and codes.
   *
   * @param  \Groundhogg\Replacements  $replacements
   *
   * @return void
   */
  function add_my_custom_replacements( \Groundhogg\Replacements $replacements ) {
  
  	$replacements->add_group( 'my_replacements', 'My Replacements' );
  
  	// code, callback, description, group
  	$replacements->add( 'my_custom_replacement', 'my_custom_replacement_callback', 'Returns the contacts username.', 'My Custom Replacement Code', 'my_replacements' );
  }
  
  /**
   * The custom replacement callback
   *
   * @param int $contact_id the contact ID
   *
   * @return string
   */
  function my_custom_replacement_callback( $contact_id ) {
  
  	$contact = \Groundhogg\get_contactdata( $contact_id );
  
  	$user_id = $contact->get_user_id();
  
  	// Generate any HTML
  	$user = get_userdata( $user_id );
  
  	return $user->user_login;
  }
  ```
- ## [Retry Failed Emails](https://groundhogg.io/codesample/retry-failed-emails/)

  Retry failed emails

  ```

  <?php
  
  add_action( 'groundhogg/event/failed', 'gh_retry_failed_email_events', 10, 2 );
  
  const GH_FAILED_EMAIL_MAX_RETRIES = 3;
  const GH_FAILED_EMAIL_RETRY_DELAY = 60 * 3; //3 minutes
  
  /**
   * Hooks in after an event fails and if the error code is wp_mail_failed retry the event in a few minutes
   *
   * @param  \Groundhogg\Event  $event
   *
   * @return void
   */
  function gh_retry_failed_email_events( \Groundhogg\Event $event, WP_Error $error ) {
  
  	// not a failed email
  	if ( $error->get_error_code() !== 'wp_mail_failed' ) {
  		return;
  	}
  	
  	// optionally target specific error messages
  	$msg = $error->get_error_message();
  	if ( 
  		! str_contains( $msg, 'some error message' )
  	) {
  		return;
  	}
  
  	$retries = $event->get_arg( '_retries', 0 );
  
  	if ( $retries >= GH_FAILED_EMAIL_MAX_RETRIES ) {
  		return;
  	}
  
  	$event->update( [
  		'status' => \Groundhogg\Event::WAITING,
  		'time'   => time() + GH_FAILED_EMAIL_RETRY_DELAY,
  	] );
  
  	$event->set_args( [ '_retries' => $retries + 1 ] );
  }
  ```
- ## [Retry failed webhook events](https://groundhogg.io/codesample/retry-failed-webhook-events/)

  Retry failed outgoing webhook events safely and quietly.

  ```

  <?php
  
  add_action( 'groundhogg/event/failed', 'gh_retry_failed_post_request_events', 10, 2 );
  
  const GH_FAILED_REQUEST_MAX_RETRIES = 3;
  const GH_FAILED_REQUEST_RETRY_DELAY = 60 * 3; //3 minutes
  
  /**
   * Hooks in after an event fails and if the error code is http_request_failed retry the event in a few minutes
   *
   * @param  \Groundhogg\Event  $event
   *
   * @return void
   */
  function gh_retry_failed_post_request_events( \Groundhogg\Event $event, WP_Error $error ) {
  
  	// not a failed request
  	if ( $error->get_error_code() !== 'http_request_failed' ) {
  		return;
  	}
  	
  	$retries = $event->get_arg( 'http_retries', 0 );
  	
  	if ( $retries >= GH_FAILED_REQUEST_MAX_RETRIES ) {
  		return;
  	}
  
  	$event->update([
  		'status' => \Groundhogg\Event::WAITING,
  		'time'   => time() + GH_FAILED_REQUEST_RETRY_DELAY,
  	]);
  	
  	$event->set_args( [ 'http_retries' => $retries + 1 ] );
  }
  ```
