Resolved

Reported for: WPML Multilingual CMS 3.3.6

Resolved in: WPML 3.4.0

Symptoms

The issue appears when you have a query for two taxonomies – similar to these examples:

http://example.com/?my-category=term&other-category=other-term
http://example.com/other-category/other-term/?my-category=term
http://example.com/my-category/term/?other-category=other-term

This leads to the following error:
PHP Catchable fatal error: Object of class WP_Error could not be converted to string in .../wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4843

Workaround

The following workaround works both for WPML versions 3.3.6 and 3.3.7, but the name of the file you need to edit is different:

  • For WPML version 3.3.6, you need to edit the sitepress.class.php file which can be found in the WPML plugin’s main folder.
  • For WPML version 3.3.7, you need to edit the class-wpml-seo-headlangs.php file, which can be found in the following location: ../sitepress-multilingual-cms/classes/seo/

You have to look for the head_langs() function (around line 4619). Replace that function with the following code:

	
function head_langs()
{
    $languages = $this->get_ls_languages( array( 'skip_missing' => true ) );
        // If there are translations and is not paged content...

        // Renders head alternate links only on certain conditions
        $the_post = get_post();
        $the_id   = $the_post ? $the_post->ID : false;
        $is_valid = count( $languages ) > 1 
        && !is_paged() 
        && ( ( ( is_single() 
        || is_page() ) 
        && $the_id 
        && get_post_status( $the_id ) == 'publish' ) 
        || ( is_home() 
        || is_front_page() 
        || is_archive() ) );

        if ( $is_valid ) {
            foreach ( $languages as $code => $lang ) {
                if (!is_wp_error($lang[ 'url' ])) { // <<< temp fix
                    $alternate_hreflang = 
                        apply_filters( 'wpml_alternate_hreflang', $lang[ 'url' ], $code );
                    printf( '' . PHP_EOL,
                        $this->get_language_tag( $code ),
                        str_replace( '&', '&', $alternate_hreflang ) 
                    );
            } // <<< temp fix
        }
    }
}

Then, in the same file, find the render_ls_li_item function (around line 3319). Replace it with the following code:

   
public function render_ls_li_item( $lang,
                            $lang_native_hidden = false,
                            $lang_translated_hidden = false,
                            $language_selected = ""
                            ) 
{
    global $icl_language_switcher_preview;
    if (!is_wp_error($lang[ 'url' ])) { //  <<< temp fix
        $country_flag_url = $lang[ 'country_flag_url' ];
        $language_url = apply_filters ( 'WPML_filter_link', $lang[ 'url' ], $lang );
        $language_flag_title = $this->settings[ 'icl_lso_display_lang' ] 
                            ? esc_attr ( $lang[ 'translated_name' ] ) 
                            : esc_attr ( $lang[ 'native_name' ] );
        $ls_settings = 
            $this->get_ls_settings ( $lang, $lang_native_hidden, $lang_translated_hidden );

        $language_selector = 
            '<li class="icl-' . $lang[ 'language_code' ]
                        . '"><a href="' . $language_url . '" ' . $language_selected . '>';

        if ( $this->settings[ 'icl_lso_flags' ] 
            || $icl_language_switcher_preview ):
            $language_selector .= 
                '<img ' . ( !$this->settings[ 'icl_lso_flags' ] 
                    ? 'style="display:none"' : '' )
                              . ' class="iclflag" '
                              . 'src="' . $country_flag_url . '" '
                              . 'alt="' . $lang[ 'language_code' ] . '" '
                              . 'title="' . $language_flag_title . '" /> ';
        endif;

        $ls_settings = $this->get_ls_settings (
            $lang,
            $ls_settings[ 'lang_native_hidden' ],
            $ls_settings[ 'lang_translated_hidden' ]
        );
        $language_selector .= icl_disp_language (
            $ls_settings[ 'lang_native' ],
            $ls_settings[ 'lang_translated' ],
            $ls_settings[ 'lang_native_hidden' ],
            $ls_settings[ 'lang_translated_hidden' ]
        );

        $language_selector
            .= '</a></li>';

        return $language_selector;
    } //  <<< temp fix
}