Skip to content

Instantly share code, notes, and snippets.

@msaari
Last active March 4, 2019 05:40
Show Gist options
  • Select an option

  • Save msaari/d6215f0e008f3445c85c26a09ea3f3db to your computer and use it in GitHub Desktop.

Select an option

Save msaari/d6215f0e008f3445c85c26a09ea3f3db to your computer and use it in GitHub Desktop.
Pick up img tag title attribute from custom field values
<?php
// Add this to theme functions.php.
add_filter( 'relevanssi_custom_field_value', 'rlv_img_alt', 10 );
function rlv_img_alt( $value ) {
if ( ! is_array( $value ) ) {
$value = array( $value );
}
$new_values = array();
foreach ( $value as $value_string ) {
if ( '<img' === substr( $value_string, 0, 4 ) ) {
$m = preg_match( '/title=[\'"](.*?)[\'"]/', $value_string, $matches );
if ( $m ) {
$value_string = $matches[1];
$value_string = str_replace( array( 'listing', 'logo', '_' ), ' ', $value_string );
}
}
$new_values[] = $value_string;
}
return $new_values;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment