Polylang Translations Menu in the Toolbar
Adds a "Translations" toolbar menu on posts and pages with links to edit each existing language version, plus links to create the missing ones.
Opens WPIDE on
Code
add_action( 'admin_bar_menu', function ( $bar ) {
if ( is_admin() || ! is_singular() || ! function_exists( 'pll_get_post_translations' ) ) {
return;
}
$id = get_queried_object_id();
if ( ! current_user_can( 'edit_post', $id ) || ! pll_is_translated_post_type( get_post_type( $id ) ) ) {
return;
}
$translations = pll_get_post_translations( $id );
$bar->add_node( array( 'id' => 'wpide-pll-tr', 'title' => esc_html__( 'Translations' ) ) );
foreach ( pll_languages_list( array( 'fields' => 'slug' ) ) as $slug ) {
if ( ! empty( $translations[ $slug ] ) ) {
$href = get_edit_post_link( $translations[ $slug ], 'raw' );
$label = strtoupper( $slug ) . ( (int) $translations[ $slug ] === $id ? ' (this page)' : '' );
} else {
$href = add_query_arg( array( 'post_type' => get_post_type( $id ), 'from_post' => $id, 'new_lang' => $slug, '_wpnonce' => wp_create_nonce( 'new-post-translation' ) ), admin_url( 'post-new.php' ) );
$label = '+ ' . strtoupper( $slug );
}
if ( $href ) {
$bar->add_node( array( 'parent' => 'wpide-pll-tr', 'id' => 'wpide-pll-tr-' . sanitize_key( $slug ), 'title' => esc_html( $label ), 'href' => $href ) );
}
}
}, 90 );
Notes
Visible only to users who can edit the post.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.