Last active
December 11, 2017 23:58
-
-
Save zackkatz/4eaafa5623d771f9156a to your computer and use it in GitHub Desktop.
GravityView - Convert links to anchors in List field output
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( 'gravityview_field_entry_value_list', 'gravityview_convert_list_items_to_links', 10, 4 ); | |
/** | |
* If there are URLs in a List field, convert them to links. Otherwise, display the original value. | |
* | |
* @param string $output HTML value output | |
* @param array $entry The GF entry array | |
* @param array $field_settings Settings for the particular GV field | |
* @param array $field The current field being processed (also exists in $field_settings) | |
*/ | |
function gravityview_convert_list_items_to_links( $output, $entry, $field_settings, $field ) { | |
$return = $output; | |
// Raw value | |
$values = unserialize( $field['value'] ); | |
$items = array(); | |
foreach ( $values as $value ) { | |
$items[] = GFCommon::is_valid_url( $value ) ? '<a href="'. esc_url( $value ). '" target="_blank">'. $value .'</a>' : $value; | |
} | |
// Generate HTML if items exist | |
if( !empty( $items ) ) { | |
$return = '<ul class="bulleted"><li>'.implode('</li><li>', $items ).'</li></ul>'; | |
} | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment