Skip to content

Instantly share code, notes, and snippets.

@certainlyakey
Created February 1, 2018 15:07
Show Gist options
  • Select an option

  • Save certainlyakey/376d24fa5ae60b3682a1a876945aab7d to your computer and use it in GitHub Desktop.

Select an option

Save certainlyakey/376d24fa5ae60b3682a1a876945aab7d to your computer and use it in GitHub Desktop.
Polylang — display flag or language code for term or post
<?php
function polylang_display_language_code_for_object($type = 'post', $id = null) {
$output = '';
if ( function_exists('pll_get_post_language') && function_exists('pll_get_term_language') ) {
// language slug
$slug = '';
if ( $type === 'post' ) {
$slug = pll_get_post_language( $id );
}
if ( $type === 'term' ) {
$slug = pll_get_term_language( $id );
}
if ( $slug ) {
$flag = file_get_contents( WP_PLUGIN_DIR . '/polylang/flags/' . ( $slug === 'en' ? 'us' : $slug ) . '.png' );
if ( $flag ) {
$output .= ' <img alt="' . __( 'Flag', 'site' ) . '" src="' . 'data:image/png;base64,' . base64_encode( $flag ) . '">';
} else {
$output .= ' (' . $slug . ')';
}
}
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment