Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00
- - - - - - -

Supporter timezone: America/Lima (GMT-05:00)

Tagged: 

This topic contains 1 reply, has 0 voices.

Last updated by Andreas W. 21 hours, 22 minutes ago.

Assisted by: Andreas W..

Author Posts
June 26, 2025 at 10:50 pm #17171814

Tory Grice

Background of the issue:
In the site I'm working on (hidden link) we use a function to add the page slug as a class to the body tag, to help with page-specific styling. However, in the WPML translated versions, the function does not create the class and add it to the body tag. This is the function: function add_slug_body_class( $classes ) { global $post; if ( isset( $post ) ) { $classes[] = $post->post_type . '-' . $post->post_name; } return $classes; } add_filter( 'body_class', 'add_slug_body_class' );

Symptoms:
The function that adds a class to the body tag based on the page slug is not working in WPML translated versions, causing styles to break.

Questions:
How can I ensure that the function works in the translated versions?

June 27, 2025 at 4:46 am #17175599

Andreas W.
WPML Supporter since 12/2018

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hello,

You will need to make use of WPML's Hooks to make sure that the translation will also be updated.

WPML Hooks:
https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

Example:

function add_slug_body_class_wpml_fallback( $classes ) {
    $original_id = get_queried_object_id();

    // Check if WPML is available
    if ( class_exists( 'sitepress' ) ) {
        $current_lang = apply_filters( 'wpml_current_language', null );
        $translated_id = wpml_object_id( $original_id, 'page', true, $current_lang );
    } else {
        $translated_id = $original_id;
    }

    // WPML logic continues
    if ( $translated_id ) {
        $post_type = get_post_type( $translated_id );
        $post_slug = get_post_field( 'post_name', $translated_id );

        if ( $post_type && $post_slug ) {
            $classes[] = $post_type . '-' . $post_slug;
        }
    }

    return $classes;
}
add_filter( 'body_class', 'add_slug_body_class_wpml_fallback' );

Note that I have not tested this snippet yet. Please make sure to take a backup of your website before running any tests and to have FPT access available in case you need to undo the changes.

Also, take note that this is a developer topic and usually out of scope for support:
https://wpml.org/purchase/support-policy/

Best regards
Andreas