Skip to content

Instantly share code, notes, and snippets.

@jacobdubail
Created March 8, 2013 01:10
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 jacobdubail/5113428 to your computer and use it in GitHub Desktop.
Save jacobdubail/5113428 to your computer and use it in GitHub Desktop.
Filter Gravity Forms checkboxes based on user_meta value
<?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