Skip to content

Instantly share code, notes, and snippets.

View roprice's full-sized avatar
🧮

Rowan roprice

🧮
View GitHub Profile
@roprice
roprice / functions.php
Created August 14, 2017 22:32
Customize the Google Map's native language to that of the current WPML language
// put this in functions.php
// this customizes the Google Maps language interface, when Toolset is being used to render Google Maps and WPML is being used to set a language
// if there is a language value set by WPML, then deregister the script enqueued in the Toolset Maps plugin, and rewrite it with a language argument
if ( ! empty( ICL_LANGUAGE_CODE ) ) {
wp_deregister_script( 'google-maps');
// where FOOBARBAZ is your Google Maps API key
wp_register_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?libraries=places&key=FOOBARBAZ&ver=3.5.2&language=' . ICL_LANGUAGE_CODE , array(), '3.5.2', true );
@roprice
roprice / functions.php
Created August 10, 2017 23:17
How to add a body class with the current WPML language
// put this code in functions.php
// add a body class with the current WPML language
add_filter('body_class', 'append_language_class');
function append_language_class($classes){
$classes[] = ICL_LANGUAGE_CODE; //or however you want to name your class based on the language code
return $classes;
}
@roprice
roprice / How to declare WPML's ICL_LANGUAGE_CODE constant as a global Javascript variable - method 2
Last active August 10, 2017 23:15
How to declare WPML's ICL_LANGUAGE_CODE constant as a global Javascript variable - method 2
/* put this code in header.php */
<?php
$currentLanguage = ICL_LANGUAGE_CODE;
$jsonCurrentLanguage = wp_json_encode($currentLanguage);
?>
<script type="text/javascript">
//<![CDATA[
var currentLanguageCode = jQuery.parseJSON( ' <?php echo $jsonCurrentLanguage; ?>' );
@roprice
roprice / How to declare WPML's ICL_LANGUAGE_CODE constant as a global Javascript variable - method 1 Raw
Last active August 10, 2017 23:14
How to declare WPML's ICL_LANGUAGE_CODE constant as a global Javascript variable - Method 1
// Put this code in functions.php
// assign the WML constant that stores the current language to a PHP variable.
$currentLanguage = ICL_LANGUAGE_CODE;
// then write javascript to DOM that declares the that variables value as a global javascript variable, for use elsewhere
echo "<script> var currentLanguage = '$currentLanguage';</script>";
@roprice
roprice / Add first and last classes to menu items in WordPress
Last active August 10, 2017 23:13
Add first and last CSS classes to all WP Menus by editing functions.php
// Put this code in functions.php
// Add first and last classes to all WordPress menu items that are part of WP Nav Menu Objects
add_filter( 'wp_nav_menu_objects', function ( $items ) {
if ( ! empty( $items ) ) {
$items[1]->classes[] = 'menu-item-first';
$items[ count( $items ) ]->classes[] = 'menu-item-last';
}
return $items;
} );