Skip to content

Instantly share code, notes, and snippets.

@kjohnson
Last active June 25, 2020 09:23
Show Gist options
  • Select an option

  • Save kjohnson/ab2cd40e6a232715d09a to your computer and use it in GitHub Desktop.

Select an option

Save kjohnson/ab2cd40e6a232715d09a to your computer and use it in GitHub Desktop.
Ninja Forms Hooks - Filters and Actions
<?php
/**
* nf_register_fields
*
* @param fields array [ filename => new FieldClass ]
*/
self::$instance->fields = apply_filters( 'nf_register_fields', self::load_classes( 'Fields' ) );
/*
* Example
*/
class MyCustomField extends NF_Abstracts_Field { ... }
add_filter( 'nf_register_fields', 'my_nf_register_fields_filter' );
function my_nf_register_fields_filter( $fields ){
$fields[ 'my-custom-field' ] = new MyCustomField;
return $fields;
}
<?php
/**
* nf_register_actions
*
* @param actions array [ filename => new ActionClass ]
*/
self::$instance->actions = apply_filters( 'nf_register_actions', self::load_classes( 'Actions' ) );
/*
* Example
*/
class MyCustomAction extends NF_Abstracts_Action { ... }
add_filter( 'nf_register_actions', 'my_nf_register_actions_filter' );
function my_nf_register_actions_filter( $actions ){
$actions[ 'my-custom-action' ] = new MyCustomAction;
return $actions;
}
<?php
/**
* nf_localize_fields
*
* @param field NF_Database_Models_Field
*/
$field = apply_filters( 'nf_localize_fields', $field );
/*
* Example
*/
add_filter( 'nf_localize_fields', 'my_nf_localize_fields_filter' );
function my_nf_localize_fields_filter( $field ){
if( 1 == $field->get_id() ){
$field->update_setting( 'required', 0 );
}
return $field;
}
<?php
/**
* nf_localize_fields_preview
*
* @param field array
*/
$field = apply_filters( 'nf_localize_fields_preview', $field );
/*
* Example
*/
add_filter( 'nf_localize_fields', 'my_nf_localize_fields_filter' );
function my_nf_localize_fields_filter( $field ){
if( 1 == $field['settings'][ 'id' ] ){
$field['settings'][ 'required' ] = 0;
}
return $field;
}
<?php
/**
* nf_field_template_file_paths
*
* @param array filepaths
*/
// Build File Path Hierarchy
$file_paths = apply_filters( 'nf_field_template_file_paths', array(
get_template_directory() . '/ninja-forms/templates/',
));
/*
* Example
*/
add_filter( 'nf_field_template_file_paths', 'my_nf_field_tempalate_file_paths' );
function my_nf_field_tempalate_file_paths( $file_paths ){
$file_paths[] = plugin_dir_path( __FILE__ ) . 'includes/my-field-template-folder/';
return $file_paths;
}
<?php
/**
* nf_output_templates
*/
do_action( 'nf_output_templates' );
/*
* Example
*/
add_action( 'nf_output_templates', 'my_custom_template_output' );
function my_custom_template_output()
{
?>
<script id="nf-tmpl-my-custom-output" type="text/template">
This is my custom tempalte.
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment