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.

This topic contains 4 replies, has 2 voices.

Last updated by berryP-2 1 year, 7 months ago.

Assigned support staff: Yvette.

Author Posts
February 18, 2018 at 11:41 pm #1521440

berryP-2

I am trying to: get option values from the default language in a (custom plugin)

Link to a page where the issue can be seen: n/a

I expected to see: a returned current language code

Instead, I got: nothing

-=-

As described on hidden link I used this to retrieve the values from the default language. This works perfectly in my theme, but now I want to use it in my own plugin as well.

I just noticed that ICL_LANGUAGE_CODE is not defined, when I try to set the language back to the 'real language'.

I noticed the filter 'acf/settings/current_language' is not reached but apart from that, doesn't seem to be defined whether it's inside or outside the filter.

I checked to see if my plugin wasn't sneaky loaded before WPML but that would seem hardly likely otherwise it would throw an error already when I set it to the default language. Is there something I'm overlooking/missing ?

This is my code:

if ( class_exists( 'SitePress' ) ) {
    /**
     * Set language to default
     *
     * @return bool|mixed
     */
    function sdmb_set_default_lang() {
        global $sitepress;
        return $sitepress->get_default_language();
    }
    add_filter( 'acf/settings/current_language', 'sdmb_set_default_lang' );
}

$email_template = ( false != get_field( 'sd_email_template', 'option' ) ) ? get_field( 'sd_email_template', 'option' ) : false;
$email_styling  = ( false != get_field( 'sd_email_style', 'option' ) ) ? get_field( 'sd_email_style', 'option' ) : false;
$email_subject  = ( false != get_field( 'sd_manual_order_email_subject_user', 'option' ) ) ? get_field( 'sd_manual_order_email_subject_user', 'option' ) : false;
$email_message  = ( false != get_field( 'sd_manual_order_email_user', 'option' ) ) ? get_field( 'sd_manual_order_email_user', 'option' ) : false;
$from_email     = ( false != get_field( 'sd_support_email', 'option' ) ) ? get_field( 'sd_support_email', 'option' ) : false;
$email_content  = str_replace( '%email_message%', $email_message, $email_template );
$site_name      = get_bloginfo( 'name' );
$home_url       = get_option( 'home' );

$bank_info = array(
    'bank' => ( false != get_field( 'sd_bank_name', 'option' ) ) ? get_field( 'sd_bank_name', 'option' ) : false,
    'iban' => ( false != get_field( 'sd_iban', 'option' ) ) ? get_field( 'sd_iban', 'option' ) : false,
    'bic'  => ( false != get_field( 'sd_bic', 'option' ) ) ? get_field( 'sd_bic', 'option' ) : false,
    'name' => ( false != get_field( 'sd_account_holder', 'option' ) ) ? get_field( 'sd_account_holder', 'option' ) : false,
);

if ( class_exists( 'SitePress' ) ) {
    // this gets 'reached'

    /**
     * Reset to 'real' language
     *
     * @return mixed
     */
    function sdmb_reset_to_real_lang() {
        // this doesn't get reached
        return ICL_LANGUAGE_CODE;
    }
    add_filter( 'acf/settings/current_language', 'sdmb_reset_to_real_lang' );
}
February 19, 2018 at 1:11 pm #1522151

Yvette
Supporter

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

Timezone: Europe/Madrid (GMT+02:00)

Hello

Are you familiar with our WPML API hooks/filters? I would suggest that you take a closer look at the following hooks:
https://wpml.org/wpml-hook/wpml_switch_language/
https://wpml.org/wpml-hook/wpml_current_language/

Using these hooks would be the recommended way to retrieve and set the language that is active for WPML purposes.

Does this help you?

February 19, 2018 at 1:38 pm #1522195

berryP-2

Only partially, because the function which is used to 'revert' to the original language is not 'reached'. So where to hook it to ?

Or do you mean something like this ?

if ( class_exists( 'SitePress' ) ) {
    /**
     * Reset to 'real' language
     */
    function sdmb_set_current_lang() {
        return apply_filters( 'wpml_current_language' );
    }
    add_filter( 'wpml_current_language', 'sdmb_set_current_lang' );
}
February 19, 2018 at 5:27 pm #1522650

Yvette
Supporter

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

Timezone: Europe/Madrid (GMT+02:00)

I should advise you that we are very limited to the type of support we can offer to clients that are creating custom plugins. (see image).
https://wpml.org/purchase/support-policy/

What I can do is advise you on the hooks you have available and refer you to our WPML contractors and advanced users who can work through debugging your code.
https://wpml.org/?page_id=7706

Our support forum is reserved for identifying/resolving bugs in our own code and for helping our clients use our standard features.

However, I would have thought you would do something as follows keeping in mind that I am sure I don´t have the full understanding nor vision of what your plugin is trying to do. I offer only as an example:

if ( class_exists( 'SitePress' ) ) {
    /**
     * Set language to default
     *
     * @return bool|mixed
     */
    function sdmb_set_default_lang() {
      // get system default language
     $my_default_lang = apply_filters('wpml_default_language', NULL );
      
     // switch the language to the default language
     if ( !is_admin() && is_index() && !$query->is_main_query() ) {
        do_action( 'wpml_switch_language', $my_default_laguage );
       }   
    add_filter( 'acf/settings/current_language', 'sdmb_set_default_lang' );
}

and

if ( class_exists( 'SitePress' ) ) {
    // this gets 'reached'
 
    /**
     * Reset to 'real' language
     *
     * @return mixed
     */
    function sdmb_reset_to_real_lang($saved_session_language) {
        // switch the current language back to the "active session" language
        // you pass the previously saved session language from elsewhere

        do_action( 'wpml_switch_language', $saved_session_laguage );
    }
    add_filter( 'acf/settings/current_language', 'sdmb_reset_to_real_lang' );
}

Kind regards

February 21, 2018 at 9:51 am #1524633

berryP-2

Taking a different route, without this.