Created
July 17, 2017 04:40
-
-
Save westonruter/b2fdf9347c8441ba9933569b15e2faa7 to your computer and use it in GitHub Desktop.
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
var wpse273548controls = (function ( api ) { | |
'use strict'; | |
var component = {}; | |
/** | |
* Init. | |
* | |
* @returns {void} | |
*/ | |
component.init = function() { | |
api.bind( 'ready', component.ready ); | |
}; | |
/** | |
* Ready. | |
* | |
* @returns {void} | |
*/ | |
component.ready = function() { | |
api.previewedDevice.bind( component.sendPreviewedDevice ); | |
api.previewer.bind( 'ready', component.sendPreviewedDevice ); | |
}; | |
/** | |
* Handle change of previewed device. | |
* @returns {void} | |
*/ | |
component.sendPreviewedDevice = function() { | |
api.previewer.send( 'previewed-device', api.previewedDevice.get() ); | |
}; | |
return component; | |
}) ( wp.customize ); |
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
var wpse273548preview = (function ( api , $) { | |
'use strict'; | |
var component = { | |
previousPreviewedDevice: null | |
}; | |
/** | |
* Init. | |
* | |
* @returns {void} | |
*/ | |
component.init = function() { | |
api.bind( 'preview-ready', function() { | |
api.preview.bind( 'previewed-device', component.handlePreviewedDeviceChange ); | |
} ); | |
}; | |
/** | |
* Handle previewed device change. | |
* | |
* @param {string} previewedDevice Previewed device. | |
* @returns {void} | |
*/ | |
component.handlePreviewedDeviceChange = function( previewedDevice ) { | |
var body = $( document.body ); | |
if ( component.previousPreviewedDevice ) { | |
body.removeClass( component.previousPreviewedDevice ); | |
} | |
body.addClass( previewedDevice ); | |
component.previousPreviewedDevice = previewedDevice; | |
}; | |
return component; | |
}) ( 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
<?php | |
/** | |
* Plugin Name: Toggle class name on body based on previewed device in Customizer | |
* Plugin URI: https://wordpress.stackexchange.com/questions/273548/detect-device-change-desktop-tablet-or-mobile-in-customizer | |
* Author: Weston Ruter, XWP | |
* Author URI: https://make.xwp.co/ | |
* License: GPLv2+ | |
*/ | |
/* | |
* Copyright (c) 2017 XWP (https://make.xwp.co/) | |
* | |
* This program is free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License, version 2 or, at | |
* your discretion, any later version, as published by the Free | |
* Software Foundation. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program; if not, write to the Free Software | |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
// Add controls script. | |
add_action( 'customize_controls_enqueue_scripts', function() { | |
$handle = 'wpse-273548-controls'; | |
$src = plugin_dir_url( __FILE__ ) . '/wpse-273548-controls.js'; | |
$deps = array( 'underscore', 'customize-controls' ); | |
wp_enqueue_script( $handle, $src, $deps ); | |
wp_add_inline_script( $handle, 'wpse273548controls.init();' ); | |
} ); | |
// Add preview script. | |
add_action( 'wp_enqueue_scripts', function() { | |
global $wp_customize; | |
if ( ! is_customize_preview() ) { | |
return; | |
} | |
$handle = 'wpse-273548-preview'; | |
$src = plugin_dir_url( __FILE__ ) . '/wpse-273548-preview.js'; | |
$deps = array( 'underscore', 'customize-preview' ); | |
wp_enqueue_script( $handle, $src, $deps ); | |
wp_add_inline_script( $handle, 'wpse273548preview.init();' ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment