Register a custom replacement code and group

📅 Published on

📝 Last updated

Documentation

»

Code Samples

»

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;
}

Was this helpful?

Let us know if this document answered your question. That’s the only way we can improve.