Wordpress filter locale and add_action->switch_to_locale

950 Views Asked by At

Changing locale via add_action->switch_to_locale works while add_filter doesn't.

add_filter

function change_locale( $locale ) {
if (is_singular()) {
        global $post;
        if(($post->ID=='1839') || ($post->ID=='577') || ($post->ID=='1346')){
            $locale = 'en_US';
        }
    }
    return $locale;
}
add_filter('locale', 'change_locale');

add_action

function change_to_english_local() {
    if (is_singular()) {
        global $post;
        if(($post->ID=='1839') || ($post->ID=='577') || ($post->ID=='1346')){
            switch_to_locale('en_US');
        }
    }
}
add_action( 'wp_enqueue_scripts', 'change_to_english_local');

Why?

0

There are 0 best solutions below