1

Is this the correct way to pass data from customize-controls.js to customize-preview.js?

( function( api ) {

api.controlConstructor['typography'] = api.Control.extend( {
    ready: function() {
        var control = this;

        function addGoogleFont(fontName) {
                var font = control.params.ogf_fonts[fontName];
                var weights = $.map(font.variants, function(value, key) {
                  return key;
                });
                var weightsURL = weights.join(',');
                var fontURL = font.family.replace(' ','+') + ':' + weightsURL;
                wp.customize.previewer.send( 'olympusFontURL', "<link href='https://fonts.googleapis.com/css?family=" + fontURL + "' rel='stylesheet' type='text/css'>" );
        }

        control.container.on( 'change', '.typography-font-family select',
            function() {
                var value = jQuery( this ).val();
                control.settings['family'].set( value );
                if( value != 'default' ) {

                    addGoogleFont( value );

                    var font = control.params.ogf_fonts[value];
                    var weightsSelect = jQuery( '.typography-font-weight select' );
                    var newWeights = font.variants;
                    weightsSelect.empty();
                    $.each( newWeights, function( key, val ) {
                        weightsSelect.append( $( "<option></option>" )
                             .attr( "value", key ).text( val ) );
                    });
                }
            }
        );

    }
} );

} )( wp.customize );

Also is this better than trying to .append() to the preview iframe directly in customize-controls.js?

Here is customize-preview.js for anyone interested:

jQuery( document ).ready( function() {

    wp.customize.bind( 'preview-ready', function() {
      wp.customize.preview.bind( 'olympusFontURL', function( url ) {
         console.log(url);
         $("head").append(url);
      } );
    } );

} ); // jQuery( document ).ready
4

Customizer Message Passing

What follows are some snippets that show the required order of operations for getting the message-passing handshakes to work. In your example code, the control is sending the message on change so it's likely that this will not happen before the overall ready event and it should be ok.

Pane → Preview

/* Pane, enqueue w/ customize-controls dependency at customize_controls_enqueue_scripts */
wp.customize.bind( 'ready', function() {
  wp.customize.previewer.bind( 'ready', function() {
     wp.customize.previewer.send( 'greeting', 'Howdy, Preview!' );
  } );
} );

/* Preview, enqueue /w customize-preview dependency at wp_enqueue_scripts if is_customize_preview() */
wp.customize.bind( 'preview-ready', function() {
  wp.customize.preview.bind( 'greeting', function( message ) {
     console.info( Pane sent message:', message );
  } );
} );

Preview → Pane

/* Preview, enqueue /w customize-preview dependency at wp_enqueue_scripts if is_customize_preview() */
wp.customize.bind( 'preview-ready', function() {
  wp.customize.preview.bind( 'active', function() {
     wp.customize.preview.send( 'greeting', 'Howdy, Pane!' );
  } );
} );

/* Pane, enqueue w/ customize-controls dependency at customize_controls_enqueue_scripts */
wp.customize.bind( 'ready', function() {
  wp.customize.previewer.bind( 'greeting', function( message ) {
     console.info( 'Preview sent greeting:', message );
  } );
} );

Suggestion

Instead of adding a change event listener for the select element via jQuery, the select element should rather be linked with a setting which you listen to instead. So your ready method should really look more like this:

this.setting.bind( function( newFont ) {
    if ( 'default' !== newFont ) {
        addGoogleFont( newFont );
    }
} );

But this addGoogleFont is passing the entire link tag to the preview, in addition to the setting change being sent. Wouldn't it be better to move all of the addGoogleFont logic into the preview itself? For example:

wp.customize( fontSettingId, function( setting ) {
    setting.bind( function( newFont ) {
        if ( 'default' !== newFont ) {
            addGoogleFont( newFont );
        }
    } );
} )

So then you just listen to the change to the font setting change in the preview, then construct the link tag to update in the preview there.

2
  • Thanks Weston! The typography control is used three times, so my understanding is that if I bind it to the select in customize-controls.js I only have to write that code once. If I moved that to preview.js, I would need to add it to the bind code for each setting, right? – Danny Cooper Apr 23 '18 at 11:25
  • Yes, that is correct. – Weston Ruter Apr 23 '18 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.