最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Customizer - binding jQuery created controls

programmeradmin1浏览0评论

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
  • 1 I do not see any jQuery controls in your question to debug, can you update your question using the edit link? – Tom J Nowell Commented Nov 9, 2020 at 1:01
  • Did you also create the setting? Creating a controls is one thing, but binding to a value only works if you've first created the setting itself. – Aristeides Commented Nov 9, 2020 at 10:06
Add a comment  | 

2 Answers 2

Reset to default 0

Thanks Tom,

Example:

  1. Just download the plugin WPSE 286375: A dynamic dropdown-pages control from here:

  2. Extract next two files in the wp plugin folder: (wp-contents/plugins)

    wpse-286375-controls.js and 
    wpse-286375.php
    
  3. Activate WPSE 286375 plugin

  4. 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',
      }) ) );};
    
  5. 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.

  6. 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!

发布评论

评论列表(0)

  1. 暂无评论