I tried several plugins posted by Weston Ruter for jQuery created controls for the WP Customizer. They work but are different from those created via PHP.
For example, controls created with PHP (customizer.php
) respond normally to code in customize-controls.js
or in customize-previews.js
:
api( 'tzkmx_test_control', function( value ){
value.bind( function( to ) {
var answer = to;
});
});
Controls created with jQuery do not respond to binding! Does anyone know how to bind them?
I tried several plugins posted by Weston Ruter for jQuery created controls for the WP Customizer. They work but are different from those created via PHP.
For example, controls created with PHP (customizer.php
) respond normally to code in customize-controls.js
or in customize-previews.js
:
api( 'tzkmx_test_control', function( value ){
value.bind( function( to ) {
var answer = to;
});
});
Controls created with jQuery do not respond to binding! Does anyone know how to bind them?
Share Improve this question edited Nov 9, 2020 at 1:00 Ivan Shatsky 8901 gold badge7 silver badges12 bronze badges asked Nov 9, 2020 at 0:31 LingoLingo 1 2 |2 Answers
Reset to default 0Thanks Tom,
Example:
Just download the plugin WPSE 286375: A dynamic dropdown-pages control from here:
Extract next two files in the wp plugin folder: (wp-contents/plugins)
wpse-286375-controls.js and wpse-286375.php
Activate WPSE 286375 plugin
Go to wp dashboard/Customize/Homepage Settings
there are 2 controls - Homepage and Posts Page with control's IDs "page_on_front" and "page_for_posts"
and
third control - Featured Page (it is from the activated plugin) with control ID "special_page"
It is created with jQuery in wpse-286375-controls.js via:
component.addControl = function() { api.control.add( new api.Control( 'special_page', _.extend( {}, component.defaultParams, { type: 'dropdown-pages', section: 'static_front_page', }) ) );};
Copy next code in your working project in your file customize-controls.js and debug it with chrome/firefox:
wp.customize('page_on_front', function( value ) { // Listen to value changes. value.bind( function( to ) { var answer = to; });});
Try to change something in Homepage control and voila the bind works and in var answer we can see the page ID of Home page.
Now change the ID of Featured Page only:
wp.customize('special_page', function( value ) { // Listen to value changes. value.bind( function( to ) { var answer = to; });});
Try to change something in Featured Page control -> No bind and Nothing happen!
Here's the 5ervant's solution that I found:
parent.wp.customize.control( 'special_page', function( control ) {
control.setting.bind( function( to ) {
var answer = to;
});});
Thanks!
edit
link? – Tom J Nowell ♦ Commented Nov 9, 2020 at 1:01