Last active
February 27, 2019 15:00
-
-
Save kellenmace/d3fa30ccabc4139cebdb56caa501597e to your computer and use it in GitHub Desktop.
Get all Gravity Forms with CSS Class. Blog post is here: https://kellenmace.com/get-all-gravity-forms-with-css-class/
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 | |
/** | |
* Get a list of Gravity Forms whose fields have a CSS class. | |
* | |
* @param string $css_class The CSS class to search for. | |
* | |
* @return array The list of forms in this format: [<form ID> - <form title>] => <array of fields that have CSS class> | |
*/ | |
function get_gravity_forms_with_css_class( $css_class ) { | |
return array_reduce( GFAPI::get_forms(), function( $forms_with_css_class, $form ) use ( $css_class ) { | |
$fields_with_class = get_gravity_form_fields_with_css_class( $form, $css_class ); | |
if ( $fields_with_class ) { | |
$forms_with_css_class[ "{$form['id']} - {$form['title']}" ] = $fields_with_class; | |
} | |
return $forms_with_css_class; | |
}, [] ); | |
} | |
/** | |
* Get all fields in a Gravity Form that have a CSS class. | |
* | |
* @param array $form The Gravity Form data. | |
* @param string $css_class The CSS class to search for. | |
* | |
* @return array Fields that have the CSS class or empty array if none. | |
*/ | |
function get_gravity_form_fields_with_css_class( $form, $css_class ) { | |
return array_filter( $form['fields'], function( $field ) use ( $css_class ) { | |
return in_array( $css_class, explode( ' ', $field->cssClass ), true ); | |
} ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment