Last active
July 5, 2017 07:42
-
-
Save soderlind/1908634f5eb0c1f69428666dd2a291d0 to your computer and use it in GitHub Desktop.
Polylang: Set WordPress Customizer preview url
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
<?php | |
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/dss-add-lang-to-template.js'; | |
$deps = array( 'customize-controls' ); | |
$version = rand(); | |
$in_footer = 1; | |
wp_enqueue_script( 'dss-add-lang-to-template', $src, $deps, $version , $in_footer ); | |
$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' | |
); | |
} |
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 ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment