Created
March 8, 2013 01:10
-
-
Save jacobdubail/5113428 to your computer and use it in GitHub Desktop.
Filter Gravity Forms checkboxes based on user_meta value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( "gform_field_content", "mz_disable_completed_series", 10, 5 ); | |
function mz_disable_completed_series( $content, $field, $value, $lead_id, $form_id ) { | |
//only manipulating checkboxes for form id 1 | |
if ( $form_id != 1 ) | |
return $form; | |
// only interested in field id 1 | |
if ( $field['id'] != 1 ) | |
return $form; | |
// get current user (users must be logged in to view the page/form) | |
$user = wp_get_current_user(); | |
// Get our items that we need to disable | |
// returns an array of id's that match checkbox values | |
$completed_series = get_user_meta( $user->ID, '_completed_series', true ); | |
/* | |
prints: | |
Focus = 7 | |
Motivation = 5 | |
*/ | |
foreach ( $completed_series as $c ) { | |
$term = get_term_by( 'id', $c, 'performance_factor' ); | |
echo $term->name . " = " . $term->term_id . "<br>"; | |
} | |
// These checks were taken from the developer docs | |
// Not sure that we need them | |
if ( rgar( $field, "type" ) == "checkbox" ) { | |
if ( RG_CURRENT_VIEW == "entry" ) { | |
} | |
else { | |
// loop over field choices | |
foreach ( $field['choices'] as $c ) { | |
// we only want to match field choices whose value is in our user_meta array | |
if ( in_array( $c['value'], $completed_series) ) { | |
echo "MATCH: " . $c['value'] . " " . $c['text'] . "<br>"; | |
// MATCH: 7 Focus | |
// MATCH: 5 Motivation | |
// WHAT DO I DO NOW? How do I either: | |
// 1. Add the disabled attribute to the field | |
// 2. Add a class or some trigger so I can disable via js | |
// 3. Remove the field, though this is far less ideal | |
} | |
} | |
} | |
} | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment