Created
February 1, 2018 15:07
-
-
Save certainlyakey/376d24fa5ae60b3682a1a876945aab7d to your computer and use it in GitHub Desktop.
Polylang — display flag or language code for term or post
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 | |
| 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