Skip to content

Instantly share code, notes, and snippets.

@kjohnson
Last active February 13, 2024 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjohnson/6a4d31f814c974eb8f5f618181ddde5a to your computer and use it in GitHub Desktop.
Save kjohnson/6a4d31f814c974eb8f5f618181ddde5a to your computer and use it in GitHub Desktop.
Ninja Forms Custom Merge Tags
<?php
/*
* Register the new merge tag class on the `ninja_forms_loaded` hook.
*/
add_action( 'ninja_forms_loaded', 'my_register_merge_tags' );
function my_register_merge_tags(){
require_once 'class.mergetags.php';
Ninja_Forms()->merge_tags[ 'my_merge_tags' ] = new My_MergeTags();
}
<?php
class My_MergeTags extends NF_Abstracts_MergeTags
{
/*
* The $id property should match the array key where the class is registered.
*/
protected $id = 'my_merge_tags';
public function __construct()
{
parent::__construct();
/* Translatable display name for the group. */
$this->title = __( 'My Merge Tags', 'ninja-forms' );
/* Individual tag registration. */
$this->merge_tags = array(
'foo' => array(
'id' => 'foo',
'tag' => '{my:foo}', // The tag to be used.
'label' => __( 'Foo', 'my_plugin' ), // Translatable label for tag selection.
'callback' => 'foo' // Class method for processing the tag. See below.
),
);
/*
* Use the `init` and `admin_init` hooks for any necessary data setup that relies on WordPress.
* See: https://codex.wordpress.org/Plugin_API/Action_Reference
*/
add_action( 'init', array( $this, 'init' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
}
public function init(){ /* This section intentionally left blank. */ }
public function admin_init(){ /* This section intentionally left blank. */ }
/**
* The callback method for the {my:foo} merge tag.
* @return string
*/
public function foo()
{
// Do stuff here.
return 'bar';
}
}
@dcolumbus
Copy link

Any idea why no matter what I do I continue to get Fatal error: Uncaught Error: Class "NF_Abstracts_MergeTags" not found

@jcastillo-johngroup
Copy link

jcastillo-johngroup commented Dec 8, 2023

@dcolumbus NF_Abstracts_MergeTags is a class made available by Ninja forms plugin. You need to have the plugin installed.

@mrizwan47
Copy link

Is there a way to get form_data inside the foo callback function? For my use case, I need to replace custom tag based on other form data -- Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment