Last active
August 20, 2020 11:23
-
-
Save ParsonsProjects/be135c9d678322da7a0ce46af7ab12ae to your computer and use it in GitHub Desktop.
Integration of Wordpress Polylang and the Customizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'customize_register' , 'my_options' ); | |
function home_page_options( $wp_customize ) { | |
// get current lang | |
$language = (empty($_REQUEST['lang'])) ? 'en_' : $_REQUEST['lang'] . '_'; | |
$wp_customize->add_setting( $language . 'my_field', | |
array( | |
'default' => '' | |
) | |
); | |
$wp_customize->add_control( $language . 'my_field', | |
array( | |
'label' => __( 'Title', 'mytheme' ), | |
'settings' => $language . 'my_field', | |
'type' => 'text' | |
) | |
); | |
$wp_customize->get_setting( $language . 'my_field' )->transport = 'postMessage'; | |
} | |
/* | |
$language = (function_exists('pll_current_language')) ? pll_current_language() . '_' : 'en_'; | |
get_theme_mod( $language . 'my_field', '' ); | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Code from https://github.com/xwp/wp-customizer-blank-slate | |
* | |
* Learn more at: https://make.xwp.co/2016/09/11/resetting-the-customizer-to-a-blank-slate/ | |
* Copyright (c) 2016 XWP (https://make.xwp.co/) | |
*/ | |
/* global wp, jQuery */ | |
/* exported PluginCustomizer */ | |
var PSPolyLang = (function( api, $ ) { | |
'use strict'; | |
var component = { | |
data: { | |
url: null | |
} | |
}; | |
/** | |
* Initialize functionality. | |
* | |
* @param {object} args Args. | |
* @param {string} args.url Preview URL. | |
* @returns {void} | |
*/ | |
component.init = function init( home ) { | |
_.extend( component.data, home ); | |
if ( ! home || ! home.url ) { | |
throw new Error( 'Missing args' ); | |
} | |
api.bind( 'ready', function(){ | |
// console.log( home.url ); | |
api.previewer.previewUrl.set( home.url ); | |
}); | |
}; | |
return component; | |
} ( wp.customize, jQuery ) ); | |
(function( api, $ ) { | |
function updateQueryStringParameter(uri, key, value) { | |
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i"); | |
var separator = uri.indexOf('?') !== -1 ? "&" : "?"; | |
if (uri.match(re)) { | |
return uri.replace(re, '$1' + key + "=" + value + '$2'); | |
} else { | |
return uri + separator + key + "=" + value; | |
} | |
} | |
api.bind( 'ready', function(){ | |
var pll = pll_object; | |
var languages = pll['languages']; | |
var current_language = pll['current_language']; | |
var current_language_name = ''; | |
var html = '<li class="accordion-section control-section control-section-themes">'; | |
html += '<h3 class="accordion-section-title"><span class="customize-action">Active language</span>Select language'; | |
html += '<select id="pll-language-select" style="position: absolute; right: 10px; top: 50%; margin-top: -14px;">'; | |
for (var i = 0; i < languages.length; i++) { | |
var language = languages[i]; | |
var selected = (language.slug === current_language) ? 'selected=""' : ''; | |
current_language_name = (language.slug === current_language) ? language.name : 'English'; | |
html += '<option ' + selected + ' value="' + language.slug + '">' + language.name + '</option>'; | |
} | |
html += '</select>'; | |
html += '</h3>'; | |
html += '</li>'; | |
$('<span style="float: right;">Language: ' + current_language_name + '</span>').insertAfter('#customize-header-actions .spinner'); | |
$(html).insertAfter('#accordion-section-themes'); | |
$('body').on('change', '#pll-language-select', function() { | |
var language = $(this).val(); | |
window.location.href = updateQueryStringParameter(window.location.href, 'lang', language); | |
}); | |
}); | |
} ( wp.customize, jQuery ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'customize_controls_enqueue_scripts', 'add_lang_to_customizer_previewer' ); | |
/** | |
* If Polylang activated, set the preview url | |
* | |
* @author soderlind | |
* @version 1.0.0 | |
*/ | |
function add_lang_to_customizer_previewer() { | |
if ( function_exists( 'pll_current_language' ) ) { | |
$src = get_stylesheet_directory_uri() . '/js/polylang-customizer.js'; | |
$deps = array( 'customize-controls' ); | |
$version = rand(); | |
$in_footer = 1; | |
// get current lang | |
$language = (empty($_REQUEST['lang'])) ? 'en' : $_REQUEST['lang']; | |
wp_enqueue_script( 'dss-add-lang-to-template', $src, $deps, $version , $in_footer ); | |
wp_localize_script( 'dss-add-lang-to-template', 'pll_object', array( 'languages' => get_option('_transient_pll_languages_list'), 'current_language' => $language ) ); | |
$lang = pll_current_language(); | |
if ( empty( $lang ) ) { | |
$lang = pll_default_language(); | |
} | |
$url = add_query_arg( 'lang', $lang, pll_home_url( $lang ) ); | |
add_lang_to_template( $url); | |
} | |
} | |
/** | |
* Set the previewer url | |
* | |
* @author soderlind | |
* @version 1.0.0 | |
*/ | |
function add_lang_to_template( $url ) { | |
wp_add_inline_script( | |
'dss-add-lang-to-template', | |
sprintf( 'PSPolyLang.init( %s );', wp_json_encode( array( 'url' => $url ) ) ), | |
'after' | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi.
I recently published plugin just for this scenario - Add Polylang support for Customizer.
Sorry for spam 👍