Last active
January 18, 2018 10:08
-
-
Save nicomollet/4f0d96df2b47889a6453b77356f70264 to your computer and use it in GitHub Desktop.
Polylang: Display a country flag or the name of the language as a "post state" in post lists
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 | |
/** | |
* Polylang: Display a country flag or the name of the language as a "post state" | |
* | |
* @param array $post_states An array of post display states. | |
* @param \WP_Post $post The current post object. | |
* | |
* @return array A filtered array of post display states. | |
*/ | |
function polylang_display_post_states_language( $post_states, $post ) { | |
if( is_plugin_active( 'polylang/polylang.php' ) ){ | |
foreach(get_the_terms( $post, 'language' ) as $language){ | |
if(file_exists(POLYLANG_DIR . '/flags/' . $language->slug . '.png')){ | |
$post_states['polylang'] = '<img src="data:image/png;base64,' . base64_encode( file_get_contents( POLYLANG_DIR . '/flags/' . $language->slug . '.png' ) ).'">'; | |
} | |
else{ | |
$post_states['polylang'] = $language->name; | |
} | |
} | |
} | |
return $post_states; | |
} | |
add_filter( 'display_post_states', 'polylang_display_post_states_language', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment