[Support request] "Elements": hook for chat in 2 languages

Home Forums Support "Elements": hook for chat in 2 languages

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #988591
    anabella

    Hi,
    I am using a chat on my website (tawk.to) and I am using Generatepress hooks to add the code.
    My problem is: My website is in 2 languages, so I have created 2 different chats (2 different codes).
    I created 2 hooks for that, but I am having problems with the “display rules”.

    1- I would like my English chat to display in all the pages that are in English within my website, and same for the Spanish chat, but I couldn´t find a way of doing that, so I selected each page manually

    2- By doing that, I have a problem with the home page: if I go to Element> Display rule > Location > Page> and select “Home”> then the chat doesn´t really show up on my home page. If, instead, I select Location> Front page > then the chat (in 1 language) shows up but in both versions of my home page (English and Spanish).

    Could you please help me?

    #989024
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Which plugin are you using to make your site multi-language?

    We might be able to use a filter to specify which hook to use based on the current language.

    #989029
    anabella

    Hi Tom.
    Thanks for your reply.
    I use WPML

    #989294
    Tom
    Lead Developer
    Lead Developer

    Ok, so:

    1. Create two different hooks for each chat. Set the Display Rules etc.. so both chats display at the same time.

    2. Take note of each Element ID. You can find the ID (number) in your address bar as you edit the element.

    3. Use a filter:

    add_filter( 'generate_layout_element_display', function( $display, $element_id ) {
        // Target Element with 123 ID and set to false if a certain language.
        if ( 123 === $element_id ) {
            if ( 'de_DE' === get_locale() ) {
                $display = false;
            }
        }
    
        // Target Element with 456 ID and set to false if a certain language.
        if ( 456 === $element_id ) {
            if ( 'en_US' === get_locale() ) {
                $display = false;
            }
        }
    }, 10, 2 );

    Let me know if that makes sense or not 🙂

    #989423
    anabella

    Thanks Tom.

    I don´t really get it, but let´s see if I can understand it:
    – Element ID for hook related to chat in English: 3315
    – Element ID for hook related to chat in Spanish: 3314

    So, should it be like this?:

    add_filter( ‘generate_layout_element_display’, function( $display, $element_id ) {
    // Target Element with 3315 ID and set to false if a certain language.
    if ( 3315 === $element_id ) {
    if ( ‘es_ES’ === get_locale() ) {
    $display = false;
    }
    }

    // Target Element with 3314 ID and set to false if a certain language.
    if ( 3314 === $element_id ) {
    if ( ‘en_US’ === get_locale() ) {
    $display = false;
    }
    }
    }, 10, 2 );

    Is that correct? And where should I put this code?

    #989574
    David
    Staff
    Customer Support

    Hi there,

    that looks correct to me.
    This article provides how to add PHP with a child theme or the Code Snippets plugin:

    https://docs.generatepress.com/article/adding-php/

    #989587
    anabella

    Hi David,

    Thanks for your reply.
    I added the code to functions.php on my child theme, but unfortunately is it not working.

    A few doubts I have:
    – As I mentioned before, my main problem is with the home page (because if I select “Home” within “display rules” the chat doesn´t really show up, and if I select “Front page” then the chat in 1 language displays in both home pages (English and Spanish). So: I am supposed to include “front page” in the display rules of BOTH hooks> and then include the code you sent me on the PHP file? That´s what I´ve done…

    – In Elements > settings> execute PHP> I get the message “Unable to execute PHP as DISALLOW_FILE_EDIT is defined”- Could this be causing problems?

    #989592
    David
    Staff
    Customer Support
    #989594
    anabella

    thanks David, still not working though
    On the WordPress Dashboard, I get the message “DISALLOW_FILE_EDIT is defined. You should also disallow PHP execution in GP Hooks” so I guess I inserted your code correctly, but in Elements > settings> execute PHP> I still get the message “Unable to execute PHP as DISALLOW_FILE_EDIT is defined”-

    #990151
    Tom
    Lead Developer
    Lead Developer

    So: I am supposed to include “front page” in the display rules of BOTH hooks> and then include the code you sent me on the PHP file?

    Yes, that’s correct.

    With the PHP code added to your child theme, do both hooks show up on the front page still?

    #990356
    anabella

    Hi Tom,
    With the code I added to the child theme, only the chat in Spanish is showing up on the front page (even if I am visiting the English version of the home page, I can still see the chat in Spanish). That´s exactly what was happening before I added the code, so basically I can´t see any difference.

    I tried something that apparently is working, although I guess it isn´t the most practical solution:
    1- I deleted the code from the child theme
    2- I created only 1 hook with the chat in Spanish (so, basically I deleted the hook that I had for the English chat)
    3- I translated the hook, using WPML
    4- When I translate the hook (using WPML editor) I only get the chance to translate the code, so I inserted the second code, which corresponds to the chat in English
    5- By doing that, the chat showed up correctly on both front pages (Enlish and Spanish) which was excactly my problem before, but it wasn´t showing up on the rest of the English pages on my website
    6- I don´t get the change to translate the “display rules” fields, so that was causing the problem
    7- So I edited the English hook (which is actually a translation of the original hook) manually (instead of using WPML editor) and I edited the display rules in order replace the spanish pages with the english pages

    Conclusion: I guess it´s a good option to create only 1 hook, and then translate it using WPML, but for some reason the “display rules” fields are not available for translation, so I am doing that manually

    #990769
    Tom
    Lead Developer
    Lead Developer

    Ah, that’s awesome that you got it working. Just to confirm, when you translated the Element to English, the Display Rules that were set in the Spanish version didn’t come along to the English version?

    Let me know 🙂

    #991428
    anabella

    Hi Tom,

    Exactly, when I translated the Element, I only got 1 field (to insert the code) and that was it. The display rules weren´t available for translation, and I thought maybe it wasn´t necessary and would be done automatically, considering that all the pages have been translated using WPML, but it didn´t work… the chat wasn´t displaying on English pages

    #991737
    Tom
    Lead Developer
    Lead Developer

    So the entire “Display Rules” tab wasn’t there? Or it just wasn’t set to the same Display Rules the original Element had?

    #992694
    anabella

    The entire “Display Rules” wasn´t there

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.