-
BrightIdiots
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!
-
Fernando
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.
-
BrightIdiots
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?
-
Fernando
What plugin are you using for the language? Let’s see what needs to be altered for the conditional statement to work.
-
BrightIdiots
We use Gtranslate for this
-
Fernando
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'] : '';
-
BrightIdiots
Yes, we’re using the paid version. What code do I have to use in combination with Generatepress for this to work?
-
Fernando
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.
-
BrightIdiots
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"
-
David
Hi there,
is that all that gets displayed on the site with Fernandos code ?
-
BrightIdiots
It depends on the selected language:
string(0) ""
– main languagestring (2) "and"
– Englishchaîne(2) "fr"
– French -
David
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 );
-
BrightIdiots
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.
-
Fernando
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 );
-
BrightIdiots
Thanks Fernando and David, this works perfectly!
-
Fernando
You’re welcome!
- You must be logged in to reply to this topic.