Edit Translations From the Admin Bar (WPML)
Adds a "Translations" menu to the toolbar on single posts and pages with a link to edit each language version, so editors can hop between translations without searching.
Opens WPIDE on
Code
add_action( 'admin_bar_menu', function ( $bar ) {
if ( is_admin() || ! is_singular() || ! defined( 'ICL_SITEPRESS_VERSION' ) ) {
return;
}
$id = get_queried_object_id();
if ( ! current_user_can( 'edit_post', $id ) ) {
return;
}
$type = apply_filters( 'wpml_element_type', get_post_type( $id ) );
$trid = apply_filters( 'wpml_element_trid', null, $id, $type );
$translations = $trid ? (array) apply_filters( 'wpml_get_element_translations', null, $trid, $type ) : array();
if ( count( $translations ) < 2 ) {
return;
}
$bar->add_node( array( 'id' => 'wpide-wpml-tr', 'title' => esc_html__( 'Translations' ) ) );
foreach ( $translations as $code => $item ) {
$link = get_edit_post_link( (int) $item->element_id, 'raw' );
if ( $link ) {
$bar->add_node( array(
'parent' => 'wpide-wpml-tr',
'id' => 'wpide-wpml-tr-' . sanitize_key( $code ),
'title' => esc_html( strtoupper( $code ) . ( (int) $item->element_id === $id ? ' (this page)' : '' ) ),
'href' => $link,
) );
}
}
}, 90 );
Notes
Only languages that already have a translation are listed, and only for 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.