Skip to content

Instantly share code, notes, and snippets.

@yoander
Last active Sep 11, 2018
Embed
What would you like to do?
Polylang hack for category, tag translation
function before_get_post ($stm) {
if ($stm->is_category || $stm->is_tag) {
$term_lang = !empty($stm->query['lang']) ? $stm->query['lang'] : '';
if (empty($term_lang) ||
!function_exists('pll_default_language') ||
($term_lang === pll_default_language())) {
return;
}
$taxonomy = $stm->is_category ? 'category' : 'post_tag';
$slug = $stm->is_category
? "{$stm->query['category_name']}-{$term_lang}"
: "{$stm->query['tag']}-{$term_lang}";
if ($term = get_term_by('slug', $slug, $taxonomy)) {
if ($stm->is_category ) {
$stm->query_vars['category_name'] = $slug;
} else {
$stm->query_vars['tag'] = $slug;
$stm->query_vars['tag_id'] = $term->term_id;
}
}
}
}
add_action('pre_get_posts', 'before_get_post', 25, 1);
function modify_category_tag_slug($termlink, $term) {
if ('category' === $term->taxonomy || preg_match('/(post_)?tag/', $term->taxonomy)) {
return preg_replace('/-en(\/)?$/i', "$1", trim($termlink));
}
return $termlink;
}
add_filter('term_link', 'modify_category_tag_slug', 10, 3);
function modify_post_slug( $url, $post ) {
if ( $post->post_type == 'post' ) {
$categ_slug = wp_get_post_categories( $post->ID, ['fields' => 'slugs' ] );
foreach ($categ_slug as $categ_slug ) {
if ((false !== strpos($url, $categ_slug)) && (false !== strpos($categ_slug, '-en'))) {
$categ_new_slug = str_replace('-en', '', $categ_slug);
return str_replace($categ_slug, $categ_new_slug, $url);
}
}
}
return $url;
}
add_filter( 'post_link', 'modify_post_slug', 10, 3 );
function display_lang($content) {
$lang_swither_options = [
'hide_current' => 1,
'show_flags' => 1,
'echo' => 0,
'hide_if_no_translation' => 1
];
if (function_exists('pll_the_languages')) {
return '<ul class="lang-switcher">' . pll_the_languages($lang_swither_options) . '</ul>' . $content;
} else {
return $content;
}
}
//add_filter ('the_content', 'display_lang');
add_filter('pll_get_taxonomies', 'add_tax_to_pll', 10, 2);
function add_tax_to_pll($taxonomies, $hide) {
if ($hide)
unset($taxonomies['my_tax']);
else
$taxonomies['my_tax'] = 'my_tax';
return $taxonomies;
}
function nirvana_meta_before() {
global $nirvanas;
// If single page take appropriate settings
if (is_single()) {
$nirvana_blog_show = $nirvanas['nirvana_single_show'];
} else {
$nirvana_blog_show = $nirvanas['nirvana_blog_show'];
}
// Post Author
$output="";
if ($nirvana_blog_show['author']) {
$output .= sprintf( '<span class="author vcard" ><i class="crycon-author crycon-metas" title="'.__( 'Author ','nirvana'). '"></i>
<a class="url fn n" rel="author" href="%1$s" title="%2$s">%3$s</a></span>',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
sprintf( esc_attr( __( 'View all posts by %s', 'nirvana' ) ), get_the_author() ),
get_the_author()
);
}
// Post date/time
if ($nirvana_blog_show['date'] || $nirvana_blog_show['time'] ) {
$separator=''; $date=''; $time='';
if ( $nirvana_blog_show['date'] && $nirvana_blog_show['time'] ) {
$separator = " - ";
}
if ($nirvana_blog_show['date']) {
$date = get_the_date();
}
if ($nirvana_blog_show['time']) {
$time = esc_attr( get_the_time() );
}
$output .= '<span>
<i class="crycon-time crycon-metas" title="'.__("Date", "nirvana").'"></i>
<time class="onDate date published" datetime="' . get_the_time('c') . '">
<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $date . $separator . $time . '</a>
</time>
</span>'.
'<time class="updated" datetime="' . get_the_modified_date('c') . '">' . get_the_modified_date() . '</time>';
}
// Post categories
if ($nirvana_blog_show['category'] && get_the_category_list()) {
$output .= '<span class="bl_categ"><i class="crycon-folder-open crycon-metas" title="' . __("Categories", "nirvana") . '"></i>'
. get_the_category_list( ', ' ) . '</span> ' ;
}
$lang_switcher = '';
if (!(is_category() || is_home() || is_archive() || is_front_page())) {
$lang_switcher = display_lang('');
}
echo $output . ' ' . $lang_switcher;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment