Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Load/exclude scripts on specific pages/slugs (with wildcard)

  • We recently translated our site, with the structure: example.com/en/slug/ and example.com/es/slug/ etc.

    For those pages/post I want to exclude (and include) some scripts. Without adding all these pages separate, is there a way to include/exclude scripts via Elements on URLs that contain /en/ or /es/ etc…?

    Thanks!

  • Hi there,

    There’s a filter called generate_element_display you can use for this. Reference: https://docs.generatepress.com/article/generate_element_display/

    Usually, a conditional code like get_locale() == 'en' is used in conjunction with the filter for these instances.

    Example snippet:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if (1234 === $element_id && get_locale() == 'en') {
            $display = true;
        } else if (5678 === $element_id && get_locale() == 'es') {
            $display = true;
        } 
    
        return $display;
    }, 10, 2 );

    You’ll need to replace 1234 and 5678 with the Element ID of the respective Element.

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

    Getting the Element ID: https://share.getcloudapp.com/YEuDdrnQ

    You’ll need to check if that conditional statement will work for the plugin you used for the language change.

  • Thanks for your reply! Just tried adding this code, but doesn’t seem to work.

    This is what I added

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if (8790 === $element_id && get_locale() == 'en') {
            $display = false;
        } else if (8790 === $element_id && get_locale() == 'es') {
            $display = false;
        } 
    
        return $display;
    }, 10, 2 );

    Is there any other option?

  • What plugin are you using for the language? Let’s see what needs to be altered for the conditional statement to work.

  • We use Gtranslate for this

  • Are you using the paid version of that plugin? If so, they have a way to check the current plugin used through a code like this:

    $current_lang = isset($_SERVER['HTTP_X_GT_LANG']) ? $_SERVER['HTTP_X_GT_LANG'] : '';

    Reference: https://docs.gtranslate.io/en/articles/1349939-how-to-detect-current-selected-language#:~:text=Detecting%20current%20language%20for%20paid%20versions

  • Yes, we’re using the paid version. What code do I have to use in combination with Generatepress for this to work?

  • Can you first create a Hook Element with this code:

    <?php
    
    $current_lang = isset($_SERVER['HTTP_X_GT_LANG']) ? $_SERVER['HTTP_X_GT_LANG'] : '';
    var_dump($current_lang );
    
    ?>

    Hook it to before_header.

    Make sure to enable PHP in the Hook settings, and set the display rule to the entire site.

    It’s best to do this on a staging site.

    Let us know what appears on top of the page on an en and es page.

    This way, we can construct a code that would have an appropriate conditional statement.

  • I just made a staging, but Gtranslate doesn’t allow the translation there. So I just added the snippet briefly to the live site, and it mentions on /en/

    string (2) "and"

  • Hi there,

    is that all that gets displayed on the site with Fernandos code ?

  • It depends on the selected language:

    string(0) "" – main language

    string (2) "and" – English

    chaîne(2) "fr" – French

  • Ok, so if you make a note of the string values for each language then you can include them in the function like so:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        $current_lang = isset($_SERVER['HTTP_X_GT_LANG']) ? $_SERVER['HTTP_X_GT_LANG'] : '';
        if (8790 === $element_id && $current_lang == 'and') {
            // display false if lang property contains and
            $display = false;
        } 
        return $display;
    }, 10, 2 );
  • This works perfectly!

    How would I add multiple languages/ID’s to this code? Or do I copy the code and adjust the language/ID’s.

    Additional info: the ‘currentlang’ is translated: the translation of the Dutch word ‘EN’ is ‘AND’. So the language has to be ‘en’, or ‘fr’ etc.

  • Yes, just expand the code. Example code:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        $current_lang = isset($_SERVER['HTTP_X_GT_LANG']) ? $_SERVER['HTTP_X_GT_LANG'] : '';
        if (8790 === $element_id && $current_lang == 'en') {
            // display false if lang property contains en
            $display = false;
        }  else if (8791 === $element_id && $current_lang == 'fr') {
            // display false if lang property contains fr
            $display = false;
        }
        return $display;
    }, 10, 2 );
  • Thanks Fernando and David, this works perfectly!

  • You’re welcome!

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