I am trying to dynamically populate a standard text field with values from a custom taxonomy. The correct result shows when I echo it but it doesn’t return to the field. Any ideas?
I only need it to show the first value of the taxonomy. Ideally the creator of the post will only select one job type which is the name of the taxonomy. Right now when I echo $result it show up on a page but when I try to return it to the field nothing shows up. I can change the “return $result;” to "return (‘test’); and the string test shows up so I know data is getting to the field. I just don’t know why the value of $result is not showing in the field.
Thank you for the code. Can you try it with some additional logging like this:
<?php
add_filter( 'gform_field_value_jobs', 'populate_jobs' );
function populate_jobs( $value ) {
GFCommon::log_debug( __METHOD__ . '(): Post => ' . print_r( $post, true ) );
$terms = wp_get_post_terms( $post->ID , array( 'cg_jobs_job_type') );
GFCommon::log_debug( __METHOD__ . '(): Post Terms => ' . print_r( $terms, true ) );
foreach ( $terms as $term ) {
GFCommon::log_debug( __METHOD__ . "(): Setting result to term name {$term->name}." );
$result = $term->name;
}
GFCommon::log_debug( __METHOD__ . "(): Returning this result {$result}." );
return $result;
}
That will only work if you also enable logging. First enable logging on the Forms > Setting page. Set the Logging radio button to “On”. Then go to Forms > Settings > Logging and ensure that logging for Gravity Forms Core is set to “log all messages”. After testing the form with the new logging statements added to your code, and logging enabled, refer back to the core Gravity Forms log on that same page (Forms > Settings > Logging). Click the view log link.
OK, thank you. On the Forms > Settings > Logging page, can you copy and share the link to the Gravity Forms Core log? If you don’t want to share that log here, please send me a private message with the link.
If you search for any of those logging statements (like “Returning this result”) in the log, they do not appear. Did you test the form? If so, are you using the correct parameter name jobs in the form builder for that field?
I didn’t find any of those terms so I changed the return $result to return (‘test’) and ran it again and now see the terms that you are talking about. When I changed the code back it didn’t log anything.