Skip to content Skip to sidebar

This thread is resolved. Here is a description of the problem and solution.

Problem:
The function intended to add a page slug as a class to the body tag does not work on WPML translated versions of the site, causing issues with page-specific styling.
Solution:
To ensure the function works with WPML translated versions, you need to modify the function to use WPML's hooks for managing translations. Here is a step-by-step guide:

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' );

Before implementing this solution, ensure to back up your site and have FTP access ready as a precaution. This modification involves developer-level changes, which are usually beyond standard support scope. For more details on WPML's hooks, visit WPML Hooks Reference.

If this solution does not apply to your case, or if it seems outdated, we recommend opening a new support ticket. Also, check related known issues at https://wpml.org/known-issues/, verify the version of the permanent fix, and confirm that you have installed the latest versions of themes and plugins. For further assistance, please visit our support forum.

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.

Tagged: 

This topic contains 1 reply, has 0 voices.

Last updated by Andreas W. 3 weeks, 1 day 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