Skip to content

Instantly share code, notes, and snippets.

@kjohnson
Last active May 30, 2018 03:22
Show Gist options
  • Save kjohnson/ff8ff732b42173c1f49aaa4cd62c8f80 to your computer and use it in GitHub Desktop.
Save kjohnson/ff8ff732b42173c1f49aaa4cd62c8f80 to your computer and use it in GitHub Desktop.
<?php
// apply_filters('ninja_forms_render_default_value', $default_value, $field_type, $feild_settings);
add_filter( 'ninja_forms_render_default_value', 'my_change_nf_default_value', 10, 3 );
function my_change_nf_default_value( $default_value, $field_type, $field_settings ) {
if( 'textbox' == $field_type ){
$default_value = 'foo';
}
return $default_value;
}
@dwayneparton
Copy link

dwayneparton commented Jan 3, 2017

The documentation was a bit confusing vs the 2.9 docs. It took me a while to figure this out why in the example $field_type = 10, $field_settings = 3? Played around with it, and this worked:

<?php 
// apply_filters('ninja_forms_render_default_value', $default_value, $field_type, $field_id);
add_filter( 'ninja_forms_render_default_value', 'my_change_nf_default_value', 'hidden', '10' );
function my_change_nf_default_value( $default_value, $field_type, $field_settings ) {
  if( 'hidden' == $field_type ){
    $default_value = 'New Value';
  }
  return $default_value;
}

Didn't know if that would be helpful to note or not. I'm assuming perhaps there are more options that would work for both $field_type and $field_settings?

@jackzwarts
Copy link

jackzwarts commented Jan 23, 2017

Typo $feild_settings shouldnt that be $field_settings !?

I used this code and its working for the simple textarea and textbox fields, however i whant to target a select field with options pulled from my database. Can you provide a simple example.

@degie
Copy link

degie commented Feb 8, 2017

however i whant to target a select field with options pulled from my database

@jackzwarts i needed set default value for ninja form from user submission, this is how I figured out. For field type listselect and listcheckbox you need add other filter and return options, like this:

<?php 
add_filter('ninja_forms_render_options', 'list_default_value', 10, 2);
function list_default_value($options, $settings)
{
    if( ! isset( $settings[ 'key' ] ) ) return $options;

    $field_id = $settings[ 'id' ];
    $default_value_list = 'yes';
    
    // Return selected checkboxes as array
    // $default_value_list = array (0 => 'Checkbox 1', 1 => 'Checkbox 2');


    foreach( $options as $key => $option ){

        if( ! isset( $option[ 'value' ] ) ) continue;

        // Fix for checkboxes ($default_value_list is array)
        if (is_array($default_value_list)) {
            foreach ($default_value_list as $value) {
                if( $option[ 'value' ] != $value ) continue;
                $options[ $key ][ 'selected' ] = 1;
            }
        }

        if( $option[ 'value' ] != $default_value_list ) continue;

        $options[ $key ][ 'selected' ] = 1;
    }

    return $options;
}

@michelemarrionline
Copy link

michelemarrionline commented Apr 30, 2018

@degie.

how can i change a default value for a checkbox field?

if (is_user_logged_in()) {
	add_filter( 'ninja_forms_render_default_value', 'loadProfile' , 10 , 3);
	function loadProfile( $default_value, $field_type, $field_settings ) {
            //other code
	    if('allow_marketing' == $field_settings[ 'key' ]) {
	        $default_value = 'checked';
	    }

	  return $default_value;
	}
}

this is not working :(

@celestialhorizon
Copy link

Where to place this code? I can't find any documentation of it and its so confusing

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