Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save badabingbreda/25966bc4e8852f442dccface97168c6b to your computer and use it in GitHub Desktop.
Save badabingbreda/25966bc4e8852f442dccface97168c6b to your computer and use it in GitHub Desktop.
Toolbox Filter Example - advanced filter
<?php
add_filter( 'toolbox/helpers/get_acf_field/type=checkbox' , 'return_checkbox' , 20, 5 );
function return_checkbox( $string , $field_object , $value , $atts = null , $postid = null ) {
if (!$atts['display_as']) return $string;
$return = array();
foreach ($field_object['choices'] as $key=>$choice ) {
if (!is_array($value)) break;
if ( in_array( $key , $value )) array_push( $return, "{$choice}" );
}
switch ($atts['display_as']){
case "list":
if (!is_array($value)) return $string;
return'<ul><li>' . implode( '</li><li>' , $return ) . '</li></ul>';
break;
case "inline":
return implode( ',' , $return );
break;
case "returned":
return implode( '<br>' , $return );
break;
case "with_images":
$on_image = (strpos( $atts['on_image'], 'http') !== false )?$atts['on_image']:wp_get_attachment_image_url( $atts['on_image'] , 'full' );
$off_image = (strpos( $atts['off_image'], 'http') !== false )?$atts['off_image']:wp_get_attachment_image_url( $atts['off_image'] , 'full' );
// start the list
$return = '<ul style="list-style-type:none">';
foreach ($field_object['choices'] as $key=>$choice ) {
if ( is_array($value) && in_array( $key , $value )) {
$return .= sprintf( '<li><img src=\'%s\'> <span class="checkbox--value">%s</span></li>' , $on_image, $choice );
} else {
$return .= sprintf( '<li><img src=\'%s\'> <span class="checkbox--value">%s</span></li>' , $off_image, $choice );
}
}
$return .= '</ul>';
return $return;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment