Last active
March 4, 2019 05:40
-
-
Save msaari/d6215f0e008f3445c85c26a09ea3f3db to your computer and use it in GitHub Desktop.
Pick up img tag title attribute from custom field values
This file contains hidden or 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 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