• First I was going to modify the htaccess file, I need another rule for english language, now I have

    <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "https://de.test.sacconicase.com"
    </IfModule>

    can I do the following?

    <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "https://de.test.sacconicase.com"
    
    Header set Access-Control-Allow-Origin "https://en.test.sacconicase.com"
    
    </IfModule>

    Or I have to open another set of tags?

    about the wp config file I was thinking to simply add another conditional for english

    define( 'WP_SITEURL', 'https://' .  $_SERVER['HTTP_HOST']);
    
    define( 'WP_HOME', 'https://' .  $_SERVER['HTTP_HOST']);
    
    if ('de.' == substr( $_SERVER['HTTP_HOST'], 0, 3 )) {
       define ('WPLANG', 'de_DE');
    }
    
    if ('en.' == substr( $_SERVER['HTTP_HOST'], 0, 3 )) {
       define ('WPLANG', 'en_EN');
    }

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 17 total)
  • Moderator bcworkz

    (@bcworkz)

    Check the responses and examples in this SO topic.

    Thread Starter sacconi

    (@sacconi)

    Is this logic correct? I changed a function

    add_filter('locale', 'lang_by_subdomain2');
    function lang_by_subdomain2( $lang ) {
       if (0 === strpos( $_SERVER['HTTP_HOST'], 'de.')) {
          return 'de_DE';
    
    if (0 === strpos( $_SERVER['HTTP_HOST'], 'en.')){
    
    return 'en_EN';
    
     }else {
          return $lang;
       }
    
       } else {
          return $lang;
       }
    }
    Moderator bcworkz

    (@bcworkz)

    No, you don’t want to nest the conditionals. As it is the English part will never execute because it’s within the German conditional which immediately returns ‘de_DE’.

    Because a return statement terminates any further execution within the function, you can simply do something like this:

    function lang_by_subdomain2( $lang ) {
       if (0 === strpos( $_SERVER['HTTP_HOST'], 'de.')) return 'de_DE';
       if (0 === strpos( $_SERVER['HTTP_HOST'], 'en.')) return 'en_EN';
       return $lang;
    }

    This way it’s much easier to add additional languages as desired without needing to work out where the curly braces belong.

    Thread Starter sacconi

    (@sacconi)

    I tryed it but it’s not working. Or I should add something?

    Moderator bcworkz

    (@bcworkz)

    IDK, works for me. Did you retain the add filter line?
    add_filter('locale', 'lang_by_subdomain2');

    FYI, when I give examples, it’s only for the applicable portion, it is very rarely complete code. I assume (probably wrongly at times) others know enough to understand how to work my example into their overall code. Apologies for any confusion I may have caused.

    Thread Starter sacconi

    (@sacconi)

    Ok, it works. I just have to modify specific funcions such as

    add_filter('the_title', function( $title, $post_id ) {
       $locale = get_locale();
       if ('de_DE' == $locale ) {
         $title_de = get_post_meta( $post_id, 'title_de', true );
         if ( ! empty( $title_de )) {
            return $title_de;
         } else {
            return $title;
         }
       } else {
         return $title;
       }
    }, 10, 2 );

    into

    add_filter('the_title', function( $title, $post_id ) {
      $lang = substr( get_locale(), 0, 2 );
    if ('it' != $lang )
       { $title_$lang = get_post_meta( $post_id, 'title_$lang', true );
         if ( ! empty( $title_$lang )) {
            return $title_$lang;
         } else {
            return $title;
         }
       } else {
         return $title;
       }
    }, 10, 2 );

    the second sounds good?

    Moderator bcworkz

    (@bcworkz)

    Yes! That’s the right approach.

    Thread Starter sacconi

    (@sacconi)

    there must be something wrong in my function because I broke the site ??

    Moderator bcworkz

    (@bcworkz)

    I was just looking at your code conceptually before — that part’s good. Specific syntax not so much ??
    'title_$lang' must instead use double quotes instead of single: "title_$lang"

    You have variable $title_$lang in a few places. You cannot have the second $ in that. $ is only allowed once at the very start of variable names. Note that "title_$lang" is not a variable, it’s a string that has a variable component: $lang. Despite similarity the two are not the same.

    Thread Starter sacconi

    (@sacconi)

    I tryed to correct but I have still a permanent error:

    add_filter('the_title', function( $title, $post_id ) {
      $lang = substr( get_locale(), 0, 2 );
    if ('it' != $lang )
       { title_$lang = get_post_meta( $post_id,"title_$lang", true );
         if ( ! empty( title_$lang )) {
            return title_$lang;
         } else {
            return $title;
         }
       } else {
         return $title;
       }
    }, 10, 2 );
    
    Moderator bcworkz

    (@bcworkz)

    The quoted string "title_$lang" is fine. The variable name title_$lang is not, should be $title_lang for all occurrences.

    Thread Starter sacconi

    (@sacconi)

    if you mean in this line { title_$lang = get_post_meta( $post_id,"title_$lang", true ); the change into { $title_lang = get_post_meta( $post_id,"title_$lang", true ); is not working, or I have to do the same also inside function (){ ?

    Moderator bcworkz

    (@bcworkz)

    Change all occurrences of title_$lang anywhere to $title_lang except for the quoted one that’s passed to get_post_meta().

    Thread Starter sacconi

    (@sacconi)

    I’m trying to do the same for the menu items, so I will filter items in any language, but I have a permanent error, this is due to a “1 more closing parenthesis” but I cant see where is the error(s)

    add_filter('nav_menu_item_title', 'sac_de_menu_title', 20, 2 );
    function sac_de_menu_title( $title, $item_post ) {

    $lang = substr( get_locale(), 0, 2 );

    if ('it' != $lang ) {
    $title_lang = get_post_meta( $item_post->ID, "title_$lang", true );
    if ( ! empty( $title_lang )) {
    return $title_lang;
    } else {
    return $title;
    }
    } else {
    return $title;
    }
    }, 10, 2 );
    Moderator bcworkz

    (@bcworkz)

    It’s more than that, but it’s the first thing the parser saw. None of this from the last line belongs: , 10, 2 ); The last line should only be }

    You’re mixing two styles of filter hooks. Either is valid, but the styles cannot be mixed. First style, named callback:

    add_filter{'hook_name', 'callback_name', 10, 2 );
    function callback_name( $data, $more ) {
      //do something
      return $data;
    }

    Second style, anonymous callback (or “closure”):

    add_filter('hook_name', function( $data, $more ) {
      //do something
      return $data;
    }, 10, 2 );
Viewing 15 replies - 1 through 15 (of 17 total)
  • You must be logged in to reply to this topic.